XML

advertisement
Chapter 13 XML
•
•
•
•
•
•
•
•
•
•
•
Concept of XML
Simple Example of XML
XML vs. HTML in Syntax
XML Structure
DTD and CDATA Sections
Concept of SAX Processing
Download and Configure SAXP Pack
Example of SAX Parser in Java with XML
Concept of DOM Processing
Building a DOM Tree from a XML File
Concept of XSLT
Chapter 13 XML (continue)
•
•
•
•
•
•
•
Examples of Stylesheets
Concept of SOAP
Examples of Using SOAP Servers with Messages
SOAP API and SOAP Server Tomcat
Configuring SOAP API and SOAP Server Tomcat
Example of Using SOAP API with Tomcat
In-Class Exercises and Lab Assignment
Concept of XML
• What is XML?
- is eXtensible Markup Language
- Easily modifying, changing, or adding data
features due to its modular structure
- is actually “meta-language”
• Why XML?
- Simplify documents communication across
different applications and languages
- Customize data storage, arrangement, and
organization
- Easily validating, integrating, and error
detecting within an application
Concept of XML(continue)
- Data in stylesheet can be customized to any
output desired
- Any type of data can be expressed as an XML
document. XML provides rules to describe data
Simple Example of XML
<?xml version=“1.0”?>
<!– xml document declaration -->
<SHOWS>
<PERFORMANCE>
<TITLE>Fairy Princess</TITLE>
<AUTHOR/>
<DESCRIPTION>
Scratch sound with emphasis on color, texture.
</DESCRIPTION>
<DATE status=“canceled”>04/16/2002</DATE>
</PERFORMANCE>
</SHOWS>
<!-- Note: <AUTHOR/> is an empty tag without data -->
Simple Example of XML (continue)
• Execution result in Netscape 6.0:
Fairy Princess Scratch sound with emphasis on color, texture. 04/16/2002
• Execution result in Internet Explorer:
<?xml version="1.0" ?>
<!-- xml document declaration -->
- <SHOWS>
- <PERFORMANCE>
<TITLE>Fairy Princess</TITLE>
<AUTHOR />
<DESCRIPTION>Scratch sound with emphasis on color, texture.
</DESCRIPTION>
<DATE status="canceled">04/16/2002</DATE>
</PERFORMANCE>
</SHOWS>
XML vs. HTML in syntax
• Every XML document must have one root element,
i.e., <shows>…..</shows>
• Tags are case-sensitive in XML, but not in HTML
• No whitespace is permitted at the beginning of a tag
(i.e., < name> is not allowed), however whitespace
at the end of tag is allowed (i.e., <name > is okay),
but not a case in HTML
• The tag name must start with either a letter or an
underscore, but user-defined tags are not allowed in
HTML
• The tag names may contain any of following:
letters, numerals, hyphens, periods, or underscores
XML vs. HTML in Syntax(continue)
• Each tag must have an end tag, the same as HTML:
i.e, <date> …………….</date>, or just an empty
tag as <author/>, which is incorrect in HTML
• Tags must be nested
i.e., <book>…..<title> …..</book></title> is
incorrect, but is okay in HTML
• Attribute values must be enclosed in quotes
i.e., date status=canceled is incorrect
• Comments use the same HTML syntax
i.e., <!--….comments here….-->
XML Structure
• Each XML file starts with an optional prolog:
<?xml version=“1.0”?>, or
<?xml version=“1.0” encoding=UTF-8” ?>, or
<?xml version=“1.0” standalone=“yes” ?>
• Each XML file must have one and only one root
element
• Each XML file may have DTD section
(Document Type Definition) or schemas section
DTD and CDATA Sections
• What is DTD?
- Document Type Definition describes the structural
requirements of an XML document
- a DTD can define the following:
• The elements and attributes appeared in a file
• Which elements are child elements and what number, order, and
placement they must have
• The default values for elements and attributes
• Why DTD?
- Its is used to ensure that each XML document follows a
specific document model, and thus is in the exact format
required for whatever processing may come its way
DTD and CDATA Sections (continue)
• A DTD can be saved as a separate file with .dtd
file extension and be referenced by the XML file
• A DTD can be completely included within the
XML file
• Examples of DTD file
- (p. 501) – author.dtd
- (downloading file: Intro to XML and XSL) –
shows.dtd
• DTD Generator (free download):
type DTD Generator, XML in www.google.com
DTD and CDATA Sections (continue)
• What are CDATA sections?
- Are areas in which the parser doesn’t process the
XML data
• Why CDATA sections?
- is very useful when there is text in the XML file
that needs to be passed through the parser
unprocessed
- to prevent the parser from reacting to offending
characters in other languages
DTD and CDATA Sections (continue)
• An example of an element with a CDATA
section:
<EQUATION><![ CDATA[x > y && z <x && z > y? ] ]>
</EQUATION>
• General form of CDATA:
<![CDATA[ your data ] ]>
Concept of SAX Processing
• What is SAX?
- Simple API for XML in Java, now it’s called as
JAXP (Java API for XML Processing)
- Event-based model for accessing XML document
contents
• Why SAX?
- Different tags as events invoke different methods
by the SAX parser sequentially from start to finish
- This enables us to do something as a result of the
event
Download and Configure SAXP pack
• Download free from the following Sun’s site:
http://java.sun.com/xml/index.html
• The current new version is Java XML Pack Summer 02 Update Release
• After download, unzip the pack into a separate
directory, i.e, SAXP
• Set up the following environment parameter at
DOS prompt:
set SAXP_HOME=c:/SAXP/java_xml_packsummer-02_01/jaxp-1.2_01
Example of SAX Parser in Java with
XML
• A simple example without DTD (p. 504 –507)
- steps:
1. Write a XML file (authorSimple.xml)
2. Write a Java program with SAX parser (Sax.java)
3. Compile the program as (one line command):
javac –classpath .;%JAXP_HOME%\jaxp.jar; %JAXP_HOME%
\crimson.jar Sax.java
4. Execute the program:
java –classpath .;%JAXP_HOME%\jaxp.jar; %JAXP_HOME%
\crimson.jar Sax authorSimple.xml false
Example of SAX Parser in Java with
XML (continue)
• Execute the program with DTD:
java –classpath .;%JAXP_HOME%\jaxp.jar; %JAXP_HOME%
\crimson.jar Sax authorSimpleInternal.xml true
• You can execute those programs without type in the long-command
if you are using JDK1.4.0 or later version, i.e.:
– compile: javac Sax.java false
– execution: java Sax authorSimpleInternal.xml true
Concept of DOM Processing
• What is DOM?
- Stands for Document Object Model
- Is a tree structure consisting of a hierarchy of nodes
stored in memory
- Is used to represent the content and model of an XML
file
• Why DOM?
- Is standard by W3C
- DOM API in SAXP
- Locate data in XML by access nodes of the tree
- Rich interfaces and methods in DOM parser by SAXP
Building a DOM Tree from a XML File
• A simple example (p. 512) Dom.java
- steps:
1. Write a XML file (author.xml)
2. Write a Java program with the DOM parser
(Dom.java)
3. Compile the program as (one-line command):
javac –classpath .;%JAXP_HOME%\jaxp.jar;%JAXP_HOME%\
crimson.jar Dom.java
4. Execute program as:
java –classpath .;%JAXP_HOME%\jaxp.jar;%JAXP_HOME%\
crimson.jar Dom author.xml false
Building a DOM Tree from a XML File
(continue)
• Example of building a DOM tree from database
- (p. 515 – 517) MakeXml.java
- compile:
javac MakeXml.java
- execute:
java MakeXml
- note: Make sure JDBC-ODBC is registered and
the Sales table exists
Concept of XSLT
• What is XSLT?
– Stands for eXtensible Stylesheet Language for
Transformations)
– XSL for formatting and XSLT for transformations
– is based on XML rules
• Why XSL/XSLT?
- Transfer a XML document to another XML file or to
HTML file
- Specify the transformation as a stylesheet following
XML syntax
- Use templates to let processor matches against the
tags in a XML file
Examples of Stylesheets
• Example of converting XML to HTML using
stylesheet (p. 518) – authorSimple.xsl
- Steps:
1. Write the .xsl file based on the .xml file
(authorSimple.xml and authorSimple.xsl)
2. Write the java program to convert (XmlToHtml.java)
3. Compile as (one-line command):
javac –classpath .;%JAXP_HOME%/jaxp.jar; %JAXP_HOME%
/crimson.jar;%JAXP_HOME%/xalan.jar XmlToHtml.java
4. Execute as (one-line command):
java –classpath .;%JAXP_HOME%/ jaxp.jar; %JAXP_HOME%
/crimson.jar;%JAXP_HOME%/xalan.jar XmlToHtml
authorSimple.xml authorSimple.xsl authorSimple.html
Examples of Stylesheets (continue)
• Another example of converting XML to HTML
using stylesheet (p. 522) – author.xml, author.xsl
- use XmlToHtml.java to do the conversion
- steps of compilation and execution the same as
last example
Concept of SOAP
• What is SOAP?
- Stands for Simple Object Access Protocol
- Is interoperability standard for Web services
- Provides a simple mechanism for making
remote procedure calls and document exchanges
using XML
• Why SOAP?
- Easier to use
- Distributed computing
- Accessibility
- XML based protocol
Examples of Using SOAP Servers with
Messages
• Use SOAP servers and SOAP message to request
information/data in XML with Java application
- Xmethods site http://www.xmethods.com
provides many SOAP servers and SOAP message
that give us information we need to write to
obtain the information/data from the SOAP server
- SOAP message is an XML document that
contains SOAP format we need to follow in order
to send request to the server
- Example of SOAP message from ITime server
in Xmethods site (p. 528 Figure 13.23)
Examples of Using SOAP (continue)
• Example of write Java application using the SOAP
message in XML (p. 529) TrySoapTime.java
• Example of calling request method with arguments
from the command line execution
- the SOAP message from BN.com server (p. 531)
Figure 13.24
- Java application using the SOAP message in XML
(p. 531) GetPrice.java
- to execute (online command):
java GetPrice services.xmethods.net 80 /soap/servlet/
rpcrouter 0764516264
SOAP API and SOAP Server Tomcat
• SOAP API allows us to write clients without having to
write XML request explicitly
• SOAP API is free to download
• SOAP server, i.e., tomcat, is also from to use
• Author has re-written the section Using the SOAP API,
for new section, check the site:
http://www.cecs.csulb.edu/~artg/internet
• Download SOAP API 2.0 soap-bin-2.0.zip from
http://xml.apache.org
• Download SOAP server (Tomcat 4.0) from
http://jakarta.apache.org/builds/jakarta-tomcat-4.0/
Configuring SOAP API and SOAP
Server Tomcat
• Assume the SOAP API 2.0 is installed in
c:\SOAP\soap-2_2
directory, we set SOAP API home path as:
set SOAP_HOME=c:\SOAP\soap-2_2
• Assume the Tomcat 4.0 is installed in
c:\TOMCAT\jakarta-tomcat-4.0
directory, we set Tomcat home path as:
set CATALINA_HOME=C:\TOMCAT\jakarta-tomcat4.0
Configuring SOAP API and SOAP
Server Tomcat (continue)
• We need to modify the classpath that Tomcat
uses. In:
%CATALINA_HOME%\bin\catalina.bat
change the line:
SET CLASSPATH=%CP%
to:
SET
CLASSPATH=%CATALINA_HOME%\common\
lib\crimson.jar;%CP%;%SOAP_HOME%
Configuring SOAP API and SOAP
Server Tomcat (continue)
• We need also to copy:
soap.war file
from:
%SOAP_HOME%\webapps\
to:
%CATALINA_HOME%\webapps\
so, after copy it should like this:
%CATALINA_HOME%\webapps\soap.war
Example of Using SOAP API with
Tomcat
• Example of Using SOAP API without XML
- (p. 533) GetJavaPrice.java
- to compile the program (one-line command):
javac –classpath .;%SOAP_HOME%\lib\soap.jar
GetJavaPrice.java
- to execute (one-line command):
java –classpath
.;%SOAP_HOME%\lib\soap.jar;%CATALINA_HOME%\
common\lib\crimson.jar;%CATALINA_HOME%\common
\lib\mail.jar;%CATALINA_HOME%\common\lib\activatio
n.jar GetJavaPrice 1576760235
In-Class Exercises and Lab Assignment
• Do TEST YOUR UNDERSTANDING 1, 3, 5, 7,
9,11, 13, and 15
• Lab Assignment
(p. 536) 5, 7, and 8
Download