Processing XML Processing XML using XSLT Processing XML

  • Slides: 81
Download presentation
Processing XML • Processing XML using XSLT • Processing XML documents with Java (DOM)

Processing XML • Processing XML using XSLT • Processing XML documents with Java (DOM) • Next week -- Processing XML documents with Java (SAX)

Processing XML using XSLT To use James Clark’s xt program visit his site at

Processing XML using XSLT To use James Clark’s xt program visit his site at http: //www. jclark. com/ and click on XML. The following programs were tested with the command line C: >xt somefile. xml somefile. xsl resultfile. html The xt classes (and xslt processing) may also be accessed via a servlet.

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <book>

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> Input

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match =

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match = "book"> <HTML><BODY><xsl: apply-templates/></BODY></HTML> </xsl: template> <xsl: template match = "title"> <H 1><xsl: apply-templates/></H 1> </xsl: template> <xsl: template match = "author"> <H 3><xsl: apply-templates/></H 3> </xsl: template> <xsl: template match = "publisher"> <P><I><xsl: apply-templates/></I></P> </xsl: template> </xsl: stylesheet> Processing

<HTML> <BODY> <H 1>The Catcher in the Rye</H 1> <H 3>J. D. Salinger</H 3>

<HTML> <BODY> <H 1>The Catcher in the Rye</H 1> <H 3>J. D. Salinger</H 3> <P> <I>Little, Brown and Company</I> </P> </BODY> </HTML> Output

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <library>

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <library> <block> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> </block> </library> Input

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match =

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match = "book"> <HTML><BODY><xsl: apply-templates/></BODY></HTML> </xsl: template> <xsl: template match = "title"> <H 1><xsl: apply-templates/></H 1> </xsl: template> <xsl: template match = "author"> <H 3><xsl: apply-templates/></H 3> </xsl: template> <xsl: template match = "publisher"> <P><I><xsl: apply-templates/></I></P> </xsl: template> </xsl: stylesheet> The default rules matches the root, library and block elements.

<HTML> <BODY> <H 1>The Catcher in the Rye</H 1> <H 3>J. D. Salinger</H 3>

<HTML> <BODY> <H 1>The Catcher in the Rye</H 1> <H 3>J. D. Salinger</H 3> <P> <I>Little, Brown and Company</I> </P> </BODY> </HTML> The output is the same.

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <book>

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> <book>Cliff Notes on The Catcher in the Rye</book> Two books in the input

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match =

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match = "book"> <HTML><BODY><xsl: apply-templates/></BODY></HTML> </xsl: template> <xsl: template match = "title"> <H 1><xsl: apply-templates/></H 1> </xsl: template> <xsl: template match = "author"> <H 3><xsl: apply-templates/></H 3> </xsl: template> <xsl: template match = "publisher"> <P><I><xsl: apply-templates/></I></P> </xsl: template> </xsl: stylesheet> What’s the output?

<HTML> <BODY> <H 1>The Catcher in the Rye</H 1> <H 3>J. D. Salinger</H 3>

<HTML> <BODY> <H 1>The Catcher in the Rye</H 1> <H 3>J. D. Salinger</H 3> <P> <I>Little, Brown and Company</I> </P> <HTML> <BODY>Cliff Notes on The Catcher in the Rye</BODY> </HTML> Illegal HTML

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <book>

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> Input

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match =

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match = "book"> <HTML><BODY><xsl: apply-templates/></BODY></HTML> </xsl: template> <xsl: template match = "title"> <H 1><xsl: apply-templates/></H 1> </xsl: template> <xsl: template match = "author"> <H 3><xsl: apply-templates/></H 3> </xsl: template> <!-<xsl: template match = "publisher"> <P><I><xsl: apply-templates/></I></P> </xsl: template> --> </xsl: stylesheet> We are not matching on publisher.

<HTML> <BODY> <H 1>The Catcher in the Rye</H 1> <H 3>J. D. Salinger</H 3>

<HTML> <BODY> <H 1>The Catcher in the Rye</H 1> <H 3>J. D. Salinger</H 3> Little, Brown and Company </BODY> </HTML> We get the default rule matching the publisher and then printing its child.

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <book>

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> Input

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match =

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match = "book"> <HTML><BODY><xsl: apply-templates/></BODY></HTML> </xsl: template> <xsl: template match = "title"> <H 1><xsl: apply-templates/></H 1> </xsl: template> <xsl: template match = "author"> <H 3><xsl: apply-templates/></H 3> </xsl: template> <xsl: template match = "publisher"> <!-- Skip the publisher --> </xsl: template> </xsl: stylesheet> We can skip the publisher by matching and stopping the recursion.

<HTML> <BODY> <H 1>The Catcher in the Rye</H 1> <H 3>J. D. Salinger</H 3>

<HTML> <BODY> <H 1>The Catcher in the Rye</H 1> <H 3>J. D. Salinger</H 3> </BODY> </HTML>

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <shelf>

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <shelf> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> </shelf> A shelf has many books.

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match =

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match = "book"> <HTML><BODY><xsl: apply-templates/></BODY></HTML> </xsl: template> <xsl: template match = "title"> <H 1><xsl: apply-templates/></H 1> </xsl: template> <xsl: template match = "author"> <H 3><xsl: apply-templates/></H 3> </xsl: template> <xsl: template match = "publisher"> <i><xsl: apply-templates/></i> </xsl: template> </xsl: stylesheet> Will this do the job?

<HTML> <BODY> <H 1>The Catcher in the Rye</H 1> <H 3>J. D. Salinger</H 3>

<HTML> <BODY> <H 1>The Catcher in the Rye</H 1> <H 3>J. D. Salinger</H 3> <i>Little, Brown and Company</i> </BODY> </HTML> This is not what we want.

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <shelf>

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <shelf> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> Same <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> </shelf> input.

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match =

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match = "shelf"> <HTML><BODY>Found a shelf</BODY></HTML> </xsl: template> </xsl: stylesheet> Checks for a shelf and quits.

<HTML> <BODY>Found a shelf</BODY> </HTML> Output

<HTML> <BODY>Found a shelf</BODY> </HTML> Output

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <shelf>

<? xml version="1. 0" ? > <? xml-stylesheet type="text/xsl" href="demo 1. xsl"? > <shelf> <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> Same <book> <title>The Catcher in the Rye</title> <author>J. D. Salinger</author> <publisher>Little, Brown and Company</publisher> </book> </shelf> input.

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match =

<xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: template match = "shelf"> <HTML> <BODY> <b>These are a few of my favorite books</b> <table width = "640“ border = “ 5”> <xsl: apply-templates/> </table> </BODY> </HTML> </xsl: template> <xsl: template match = "book"> <tr> <td> Produce a table of <xsl: number/> </td> <xsl: apply-templates/> </tr> </xsl: template> <xsl: template match = "title | author | publisher"> <td><xsl: apply-templates/></td> </xsl: template> </xsl: stylesheet> books.

<HTML> <BODY> <b>These are a few of my favorite books</b> <table width="640“ border =

<HTML> <BODY> <b>These are a few of my favorite books</b> <table width="640“ border = “ 5”> <tr> <td>1</td> <td>The Catcher in the Rye</td> <td>J. D. Salinger</td> <td>Little, Brown and Company</td> </tr> <td>2</td> <td>The XSLT Programmer's Reference</td> <td>Michael Kay</td> <td>3</td> <td>Wrox Press</td> <td>Computer Organization and Design</td> </tr> <td>Patterson and Henessey</td> <tr> <td>Morgan Kaufmann</td> </tr> </table> </BODY> </HTML>

Processing XML Documents with Java (DOM) • The following examples were tested using Sun’s

Processing XML Documents with Java (DOM) • The following examples were tested using Sun’s JAXP (Java API for XMP Parsing. This is available at http: //www. javasoft. com/ and click on XML

XML DOM • The World Wide Web Consortium’s Document Object Model • Provides a

XML DOM • The World Wide Web Consortium’s Document Object Model • Provides a common vocabulary to use in manipulating XML documents. • May be used from C, Java, Perl, Python, or VB • Things may be quite different “under the hood”. • The interface to the document will be the same.

The XML File “cats. xml” <? xml version = "1. 0" ? > <!DOCTYPE

The XML File “cats. xml” <? xml version = "1. 0" ? > <!DOCTYPE Top. Cat SYSTEM "cats. dtd"> <Top. Cat> I am The Cat in The Hat <Little. Cat. A> I am Little Cat A </Little. Cat. A> <Little. Cat. B> I am Little Cat B <Little. Cat. C> I am Little Cat C </Little. Cat. C> </Little. Cat. B> <Little. Cat. D/> </Top. Cat>

document XML doctype Called the Document Element element topcat text element I am the

document XML doctype Called the Document Element element topcat text element I am the cat in the hat DOM Little cat A text I am little cat A I am little cat B element Little cat B Little cat D element Little Cat C text I am little cat C

Agreement. xml <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE Fixed. Float. Swap SYSTEM "Fixed.

Agreement. xml <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE Fixed. Float. Swap SYSTEM "Fixed. Float. Swap. dtd"> <Fixed. Float. Swap> <Notional>100</Notional> <Fixed_Rate>5</Fixed_Rate> <Num. Years>3</Num. Years> <Num. Payments>6</Num. Payments> </Fixed. Float. Swap>

document XML doctype Fixed. Float. Swap Notional Fixed. Rate 100 5 Num. Years Num.

document XML doctype Fixed. Float. Swap Notional Fixed. Rate 100 5 Num. Years Num. Payments 3 All of these nodes implement the Node interface 6

Operation of a Tree-based Parser XML DTD Document Tree Valid XML Document Tree-Based Parser

Operation of a Tree-based Parser XML DTD Document Tree Valid XML Document Tree-Based Parser Application Logic

Some DOM Documentation from Java. Soft

Some DOM Documentation from Java. Soft

The Node Interface • The Node interface is the primary datatype for the entire

The Node Interface • The Node interface is the primary datatype for the entire Document Object Model. • It represents a single node in the document tree. • While all objects implementing the Node interface expose methods for dealing with children, not all objects implementing the Node interface may have children. • For example, Text nodes may not have children.

Properties • All Nodes have properties. • Not all properties are needed by all

Properties • All Nodes have properties. • Not all properties are needed by all types of nodes. • The attribute property is an important part of the Element node but is null for the Text nodes. • We access the properties through methods…

Some Methods of Node Example Methods are: String get. Node. Name() – depends on

Some Methods of Node Example Methods are: String get. Node. Name() – depends on the Node type if Element node return tag name if Text node return #text

Some Methods of Node Example Methods are: short get. Node. Type() Might return a

Some Methods of Node Example Methods are: short get. Node. Type() Might return a constant like ELEMENT_NODE or TEXT_NODE or …

Some Methods of Node Example Methods are: String get. Node. Value() if the Node

Some Methods of Node Example Methods are: String get. Node. Value() if the Node is an Element Node then return ‘null’ if the Node is a Text Node then return a String representing that text.

Some Methods of Node Example Methods are: Node get. Parent. Node() returns a reference

Some Methods of Node Example Methods are: Node get. Parent. Node() returns a reference to the parent

Some Methods of Node Example Methods are: public Node get. First. Child() Returns the

Some Methods of Node Example Methods are: public Node get. First. Child() Returns the value of the first. Child property.

Some Methods of Node Example Methods are: public Node. List get. Child. Nodes() returns

Some Methods of Node Example Methods are: public Node. List get. Child. Nodes() returns a Node. List object Node. List is an interface and not a Node.

The Node. List Interface • The Node. List interface provides the abstraction of an

The Node. List Interface • The Node. List interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented. • The items in the Node. List are accessible via an integral index, starting from 0.

There are only two methods of the Node. List Interface public Node item(int index)

There are only two methods of the Node. List Interface public Node item(int index) Returns the item at index in the collection. If index is greater than or equal to the number of nodes in the list, this returns null.

There are only two methods of the Node. List Interface public int get. Length()

There are only two methods of the Node. List Interface public int get. Length() Returns the value of the length property.

The Element Interface public interface Element extends Node Inheritance • By far the vast

The Element Interface public interface Element extends Node Inheritance • By far the vast majority of objects (apart from text) that authors encounter when traversing a document are Element nodes. Nothing prevents us from extending one interface in order to create another. • Those who implement Element just have more promises to keep.

The Element Interface public interface Element extends Node • Some methods in the Element

The Element Interface public interface Element extends Node • Some methods in the Element interface String get. Attribute(String name) Retrieves an attribute value by name.

The Element Interface public interface Element extends Node • Some methods in the Element

The Element Interface public interface Element extends Node • Some methods in the Element interface public String get. Tag. Name() Returns the value of the tag. Name property.

The Element Interface public interface Element extends Node • Some methods in the Element

The Element Interface public interface Element extends Node • Some methods in the Element interface public Node. List get. Elements. By. Tag. Name(String name) Returns a Node. List of all descendant elements with a given tag name, in the order in which they would be encountered in a preorder traversal of the Element tree. .

The Character. Data Interface public interface Character. Data extends Node The Character. Data interface

The Character. Data Interface public interface Character. Data extends Node The Character. Data interface extends Node with a set of attributes and methods for accessing character data in the DOM. For clarity this set is defined here rather than on each object that uses these attributes and methods. No DOM objects correspond directly to Character. Data, though Text and others do inherit the interface from it. All offsets in this interface start from 0.

The Character. Data Interface public interface Character. Data extends Node An example method: public

The Character. Data Interface public interface Character. Data extends Node An example method: public String get. Data() Returns the value of the character data of the node that implements this interface. The Text interface extends Character. Data. public void set. Data(String data) is also available.

The Document Interface public interface Document extends Node The Document interface represents the entire

The Document Interface public interface Document extends Node The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.

The Document Interface public interface Document extends Node Some methods: public Element get. Document.

The Document Interface public interface Document extends Node Some methods: public Element get. Document. Element() Returns the value of the document. Element property. This is a convenience attribute that allows direct access to the child node that is the root element of the document. For HTML documents, this is the element with the tag. Name "HTML".

The Document Interface Some methods: public Node. List get. Elements. By. Tag. Name(String tagname)

The Document Interface Some methods: public Node. List get. Elements. By. Tag. Name(String tagname) Returns a Node. List of all the Elements with a given tag name in the order in which the would be encountered in a preorder traversal of the Document tree. Parameters: tagname - The name of the tag to match on. The special value "*" matches all tags. Returns: A new Node. List object containing all the matched Elements.

Fixed. Float. Swap. xml <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE Fixed. Float. Swap

Fixed. Float. Swap. xml <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE Fixed. Float. Swap SYSTEM "Fixed. Float. Swap. dtd"> <Fixed. Float. Swap> <Notional>100</Notional> <Fixed_Rate>5</Fixed_Rate> <Num. Years>3</Num. Years> <Num. Payments>6</Num. Payments> </Fixed. Float. Swap>

document XML doctype Fixed. Float. Swap Notional Fixed. Rate 100 5 Num. Years Num.

document XML doctype Fixed. Float. Swap Notional Fixed. Rate 100 5 Num. Years Num. Payments 3 Fixed. Float. Swap. xml 6

An Example import java. io. File; import org. w 3 c. dom. *; import

An Example import java. io. File; import org. w 3 c. dom. *; import javax. xml. parsers. Document. Builder. Factory; import javax. xml. parsers. Document. Builder; import org. xml. sax. SAXException; import org. xml. sax. SAXParse. Exception; Process a local file

public class Simulator 3 { public static void main(String argv[]) { Document doc; if(argv.

public class Simulator 3 { public static void main(String argv[]) { Document doc; if(argv. length != 1 ) { System. err. println("usage: java Simulator 3 documentname") System. exit(1); } try { Document. Builder. Factory doc. Builder. Factory = Document. Builder. Factory. new. Instance(); Document. Builder doc. Builder = doc. Builder. Factory. new. Document. Builder();

doc = doc. Builder. parse(new File(argv[0])); Element top = doc. get. Document. Element(); top.

doc = doc. Builder. parse(new File(argv[0])); Element top = doc. get. Document. Element(); top. normalize(); // concatenate adjacent text nodes Node. List element. List = top. get. Elements. By. Tag. Name("*"); int list. Length = element. List. get. Length(); for(int i = 0; i < list. Length; i++) { Element e = (Element)element. List. item(i); System. out. print(e. get. Node. Name()); Text t = (Text)e. get. First. Child(); System. out. println(t. get. Node. Value()); }

} catch(SAXParse. Exception err) { System. out. println("Parsing error" + ", line " +

} catch(SAXParse. Exception err) { System. out. println("Parsing error" + ", line " + err. get. Line. Number() + ", URI " + err. get. System. Id()); System. out. println(" " + err. get. Message()); } catch(SAXException e) { Exception x = e. get. Exception(); ((x == null) ? e : x). print. Stack. Trace(); } catch (Throwable t) { t. print. Stack. Trace(); } System. exit(0); } }

Fixed. Float. Swap. xml <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE Fixed. Float. Swap

Fixed. Float. Swap. xml <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE Fixed. Float. Swap SYSTEM "Fixed. Float. Swap. dtd"> <Fixed. Float. Swap> <Notional>100</Notional> <Fixed_Rate>5</Fixed_Rate> <Num. Years>3</Num. Years> <Num. Payments>6</Num. Payments> </Fixed. Float. Swap>

Output Notional 100 Fixed_Rate 5 Num. Years 3 Num. Payments 6

Output Notional 100 Fixed_Rate 5 Num. Years 3 Num. Payments 6

Another DOM Example A Java Program that reads Fixed. Float. Swap. xml from Jigsaw

Another DOM Example A Java Program that reads Fixed. Float. Swap. xml from Jigsaw and performs validation against the server based DTD. More on DTD’s next week. The program then displays the DOM tree.

import java. net. *; import java. io. *; import org. w 3 c. dom.

import java. net. *; import java. io. *; import org. w 3 c. dom. *; import javax. xml. parsers. Document. Builder. Factory; import javax. xml. parsers. Document. Builder; import org. xml. sax. *; public class Simulator 6 { public static void main(String argv[]) { try { Process a file on the internet. Document. Builder. Factory doc. Builder. Factory = Document. Builder. Factory. new. Instance(); doc. Builder. Factory. set. Validating(true); doc. Builder. Factory. set. Namespace. Aware(true);

Document. Builder doc. Builder = doc. Builder. Factory. new. Document. Builder(); doc. Builder. set.

Document. Builder doc. Builder = doc. Builder. Factory. new. Document. Builder(); doc. Builder. set. Error. Handler( new org. xml. sax. Error. Handler() { Register our own event handler public void fatal. Error(SAXParse. Exception e) throws SAXException { System. out. println("Fatal error"); // an exception will be thrown by SAX } public void error(SAXParse. Exception e) throws SAXParse. Exception { System. out. println("Validity error"); throw e; }

public void warning(SAXParse. Exception err) throws SAXParse. Exception { System. out. println("** Warning" +

public void warning(SAXParse. Exception err) throws SAXParse. Exception { System. out. println("** Warning" + ", line " + err. get. Line. Number() + ", uri " + err. get. System. Id()); System. out. println(" " + err. get. Message()); throw err; } } ); public interface Error. Handler Basic interface for SAX error handlers. If a SAX application needs to implement customized error handling, it must implement this interface and then register an instance with the SAX parser using the parser's set. Error. Handler method. The parser will then report all errors and warnings through this interface. The parser shall use this interface instead of throwing an exception: it is up to the application whether to throw an exception for different types of errors and warnings. Note, however, that there is no requirement that the parser continue to provide useful information after a call to fatal. Error (in other words, a SAX driver class could catch an exception and report a fatal. Error).

Input. Source is = new Input. Source("http: //mccarthy. heinz. cmu. edu: 8001/fpml/Agreement. xml"); Document

Input. Source is = new Input. Source("http: //mccarthy. heinz. cmu. edu: 8001/fpml/Agreement. xml"); Document doc = doc. Builder. parse(is); System. out. println("No Problems found"); // Let’s print the tree Tree. Printer tp = new Tree. Printer(doc); tp. print(); Under WWW/fpml Jigsaw’s port. } A single input source for an XML entity.

catch(SAXParse. Exception err) { System. out. println("Catching raised exception"); System. out. println("Parsing error" +

catch(SAXParse. Exception err) { System. out. println("Catching raised exception"); System. out. println("Parsing error" + ", line " + err. get. Line. Number() + ", URI " + err. get. System. Id()); System. out. println(" " + err. get. Message()); } catch(SAXException e) { System. out. println("Catch clause 2"); Exception x = e. get. Exception(); ((x == null) ? e : x). print. Stack. Trace(); } catch (Throwable t) { System. out. println("Catch clause 3"); t. print. Stack. Trace(); } System. exit(0); } }

Tree. Print Class import org. w 3 c. dom. *; public class Tree. Printer

Tree. Print Class import org. w 3 c. dom. *; public class Tree. Printer { private Document doc; private int current. Indent; public Tree. Printer(Document d) { current. Indent = 2; doc = d; } public void print() { private. Print(doc, current. Indent); } }

document XML doctype Fixed. Float. Swap Notional Fixed. Rate 100 5 Num. Years Num.

document XML doctype Fixed. Float. Swap Notional Fixed. Rate 100 5 Num. Years Num. Payments 3 Fixed. Float. Swap. xml 6

public void private. Print(Node n, int indent) { for(int i = 0; i <

public void private. Print(Node n, int indent) { for(int i = 0; i < indent; i++) System. out. print(" "); switch( n. get. Node. Type()) { // Print information as each node type is encountered case n. DOCUMENT_NODE : System. out. println(n. get. Node. Name() + ". . . Document No break; case n. ELEMENT_NODE : System. out. println(n. get. Node. Name() + ". . . Element Node" break; case n. TEXT_NODE : System. out. println(n. get. Node. Name() + ". . . Text Node"); break; case n. CDATA_SECTION_NODE: System. out. println(n. get. Node. Name() + ". . . CDATA Node"); break; case n. PROCESSING_INSTRUCTION_NODE: System. out. println("<? "+n. get. Node. Name()+". . . ? >"+ ". . . PI Node"); break;

case n. COMMENT_NODE: System. out. println("<!--"+n. get. Node. Value()+"-->" + ". . . Comment

case n. COMMENT_NODE: System. out. println("<!--"+n. get. Node. Value()+"-->" + ". . . Comment node"); break; case n. ENTITY_NODE: System. out. println("ENTITY "+ n. get. Node. Name()+ ". . . Entity Node"); break; case n. ENTITY_REFERENCE_NODE: System. out. println("&"+n. get. Node. Name()+"; " + ". . . Entity Reference Node"); break; case n. DOCUMENT_TYPE_NODE: System. out. println("DOCTYPE"+n. get. Node. Name()+ ". . . Document Type Node" break; default: System. out. println("? " + n. get. Node. Name()); } Node child = n. get. First. Child(); while(child != null) { private. Print(child, indent+current. Indent); child = child. get. Next. Sibling(); } } }

Output No Problems found #document. . . Document Node DOCTYPEFixed. Float. Swap. . .

Output No Problems found #document. . . Document Node DOCTYPEFixed. Float. Swap. . . Document Type Node Fixed. Float. Swap. . . Element Node #text. . . Text Node Notional. . . Element Node #text. . . Text Node Fixed_Rate. . . Element Node #text. . . Text Node Num. Years. . . Element Node #text. . . Text Node Num. Payments. . . Element Node #text. . . Text Node

Example of using XML: Web Applications Our example is an application called Power. Warning.

Example of using XML: Web Applications Our example is an application called Power. Warning. Using Power. Warning we access a weather information site on the Web to obtain the current temperature for a particular location. Based on the temperature and if certain conditions have been met, the application sends a notice to clients of the service alerting them of the condition.

We know that the weather information is available from the Web at http: //www.

We know that the weather information is available from the Web at http: //www. xweather. com/White_Plains_NY_US. html. [1] [2] [3] [4] [5] [6] [7] [8] <html> <head> <title>Weather Report</title> </head> <body> <h 2>Weather Report -- White Plains, NY </h 2> <table border=1> <tr><td>Date/Time</td><td align=center>11 AM EDT Sat Jul 25 1998</td></tr> [9] <tr><td>Current Tem. </td><td align=center>70° </td></tr> [10] <tr><td>Today’s High</td><td align=center>82° </td></tr> [11] <tr><td>Today’s Low</td><td align=center>62° </td><tr> [12] </table> [13] </body> [14] </html>

 • Strategy 1: For the current temperature of White Plains, go to line

• Strategy 1: For the current temperature of White Plains, go to line 9, column 46 of the page and continue until reaching the next ampersand. • Strategy 2: For the current temperature of the White Plains, go to the first <table> tag, then go to the second <tr> tag within the table, and then go to the second <tg> tag within the row.

<? xml version=“ 1. 0”? > <!DOCTYPE Weather. Report SYSTEM “http>//www. xweather. com/Weather. Report.

<? xml version=“ 1. 0”? > <!DOCTYPE Weather. Report SYSTEM “http>//www. xweather. com/Weather. Report. dtd”> <Weather. Report> <City>White Plains</City> <State>NY</State> <Date>Sat Jul 25 1998</Date> <Time>11 AM EDT</Time> <Curr. Temp unit=“Farenheit”>70</Curr. Temp> <High unit=“Farenheit”>82</High> <Low unit=“Farenheit”>62</Low> </Weather Report>

 • Strategy 3: For the current temperature of White Plains, N. Y. ,

• Strategy 3: For the current temperature of White Plains, N. Y. , go to the <Curr. Temp> tag.

Weather. Report application Mobile users WML HTML Common logical data (DOM/XML) PC users Http:

Weather. Report application Mobile users WML HTML Common logical data (DOM/XML) PC users Http: //www. xweather. com Power. Warning application XML Next week -- SAX Application programs

Classpath Info When I work with JAXP (DOM and SAX) my classpath contains the

Classpath Info When I work with JAXP (DOM and SAX) my classpath contains the following: c: Program FilesJava. SoftJaxp 1. 0jaxp. jar c: Program FilesJava. SoftJaxp 1. 0parser. jar Available at www. javasoft. com/xml When I work with xt (XSLT) my classpath contains the following: c: Xtxt. jar c: XPxp. jar Available at www. jclark. com/xml For Jigsaw 2. 0. 5 my classpath looks like c: Jigsawclassesjigsaw. zip c: Jigsawclassesservlet. jar c: Jigsawclassesjigadmin. jar Available at www. w 3. org/jigsaw For Jigsaw to run JSP pages, my classpath looks like this: c: gnujsp-1. 0. 1libgnujsp 10. jar Available at www. klomp. org. gnujsp