Document Sample
xml Powered By Docstoc
					              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
  - 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 -->
       <TITLE>Fairy Princess</TITLE>
             Scratch sound with emphasis on color, texture.
       <DATE status=“canceled”>04/16/2002</DATE>
<!-- 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 -->
         <TITLE>Fairy Princess</TITLE>
         <AUTHOR />
         <DESCRIPTION>Scratch sound with emphasis on color, texture.
         <DATE status="canceled">04/16/2002</DATE>
         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
• 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
• 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) –
• DTD Generator (free download):
  type DTD Generator, XML in
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
  - 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
 <EQUATION><![ CDATA[x > y && z <x && z > y? ] ]>
• 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
• 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
Download and Configure SAXP pack
• Download free from the following Sun’s site:
• 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_pack-
 Example of SAX Parser in Java with
• A simple example without DTD (p. 504 –507)
  - steps:
  1. Write a XML file (authorSimple.xml)
  2. Write a Java program with SAX parser (
  3. Compile the program as (one line command):
 javac –classpath .;%JAXP_HOME%\jaxp.jar; %JAXP_HOME%
 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 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
• Why DOM?
 - Is standard by W3C
 - 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)
  - steps:
  1. Write a XML file (author.xml)
  2. Write a Java program with the DOM parser
  3. Compile the program as (one-line command):
 javac –classpath .;%JAXP_HOME%\jaxp.jar;%JAXP_HOME%\
 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
• Example of building a DOM tree from database
  - (p. 515 – 517)
  - compile:
 - 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
  – XSL for formatting and XSLT for transformations
  – is based on XML rules
  - 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 (
 3. Compile as (one-line command):
 javac –classpath .;%JAXP_HOME%/jaxp.jar; %JAXP_HOME%
 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 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
• Use SOAP servers and SOAP message to request
  information/data in XML with Java application
  - Xmethods site
  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)
• Example of calling request method with arguments
  from the command line execution
  - the SOAP message from server (p. 531)
  Figure 13.24
  - Java application using the SOAP message in XML
  (p. 531)
  - to execute (online command):
java GetPrice 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:
• Download SOAP API 2.0 from
• Download SOAP server (Tomcat 4.0) from
  Configuring SOAP API and SOAP
           Server Tomcat
• Assume the SOAP API 2.0 is installed in
  directory, we set SOAP API home path as:
  set SOAP_HOME=c:\SOAP\soap-2_2
• Assume the Tomcat 4.0 is installed in
  directory, we set Tomcat home path as:
  set CATALINA_HOME=C:\TOMCAT\jakarta-tomcat-
 Configuring SOAP API and SOAP
     Server Tomcat (continue)
• We need to modify the classpath that Tomcat
  uses. In:
 change the line:
  Configuring SOAP API and SOAP
      Server Tomcat (continue)
• We need also to copy:
  soap.war file
 so, after copy it should like this:
   Example of Using SOAP API with
• Example of Using SOAP API without XML
  - (p. 533)
  - to compile the program (one-line command):
 javac –classpath .;%SOAP_HOME%\lib\soap.jar
 - to execute (one-line command):
 java –classpath
 ion.jar GetJavaPrice 1576760235
In-Class Exercises and Lab Assignment
  9,11, 13, and 15
• Lab Assignment
  (p. 536) 5, 7, and 8

Shared By: