Java and XML DOM and SAX Some of

  • Slides: 101
Download presentation
Java and XML (DOM and SAX) Some of the material for these slides came

Java and XML (DOM and SAX) Some of the material for these slides came from the following sources: “XML a Manager’s Guide” by Kevin Dick “The XML Companion” by Bradley Java Documentation from Sun Microsystems “XML and Java” by Maruyama, Tamura and Uramoto On and Off the internet… Internet Technologies

Java and XML (DOM and SAX) • Parser Operations with DOM and SAX overview

Java and XML (DOM and SAX) • Parser Operations with DOM and SAX overview • Processing XML with SAX (locally and on the internet) • Processing XML with DOM (locally and on the internet) Internet Technologies

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> Internet Technologies

Fixed. Float. Swap. dtd <? xml version="1. 0" encoding="utf-8"? > <!ELEMENT Fixed. Float. Swap

Fixed. Float. Swap. dtd <? xml version="1. 0" encoding="utf-8"? > <!ELEMENT Fixed. Float. Swap (Notional, Fixed_Rate, Num. Years, Num. Payments ) > <!ELEMENT Notional (#PCDATA) > <!ELEMENT Fixed_Rate (#PCDATA) > <!ELEMENT Num. Years (#PCDATA) > <!ELEMENT Num. Payments (#PCDATA) > Internet Technologies

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

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

Tree Benefits • Some data preparation tasks require early access to data that is

Tree Benefits • Some data preparation tasks require early access to data that is further along in the document (e. g. we wish to extract titles to build a table of contents) • New tree construction is easier (e. g. XSLT works from a tree to convert Fp. ML to WML) Internet Technologies

Operation of an Event Based Parser XML DTD Valid Application Logic Event-Based Parser XML

Operation of an Event Based Parser XML DTD Valid Application Logic Event-Based Parser XML Document Internet Technologies

Operation of an Event Based Parser XML DTD Valid public void start. Document ()

Operation of an Event Based Parser XML DTD Valid public void start. Document () public void end. Document () public void start. Element (…)) public void end. Element (…) public void characters (…)) Event-Based Parser XML Document Application Logic public void error(SAXParse. Exception e) throws SAXException { System. out. println("nn--Invalid document ---" + e); } Internet Technologies

Event-Driven Benefits • We do not need the memory required for trees • Parsing

Event-Driven Benefits • We do not need the memory required for trees • Parsing can be done faster with no tree construction going on Internet Technologies

XML API’s w/jaxpack Internet Technologies

XML API’s w/jaxpack Internet Technologies

Important SAX interfaces and classes class Input. Source -- A single input source for

Important SAX interfaces and classes class Input. Source -- A single input source for an XML entity interface XMLReader -- defines parser behavior (implemented by Xerces’ SAXParser) Four core SAX 2 handler interfaces: • Entity. Resolver • DTDHandler • Content. Handler • Error. Handler Implemented by class Default. Handler Internet Technologies

Processing XML with SAX interface XMLReader -- defines parser behavior (implemented by Xerces’ SAXParser)

Processing XML with SAX interface XMLReader -- defines parser behavior (implemented by Xerces’ SAXParser) XMLReader is the interface that an XML parser's SAX 2 driver must implement. This interface allows an application to set and query features and properties in the parser, to register event handlers for document processing, and to initiate a document parse. Internet Technologies

Processing XML with SAX • We will look at the following interfaces and classes

Processing XML with SAX • We will look at the following interfaces and classes and then study an example interface Content. Handler -- reports on document events interface Error. Handler – reports on validity errors class Default. Handler – implements both of the above plus two others Internet Technologies

public interface Content. Handler Receive notification of general document events. This is the main

public interface Content. Handler Receive notification of general document events. This is the main interface that most SAX applications implement: if the application needs to be informed of basic parsing events, it implements this interface and registers an instance with the SAX parser using the set. Content. Handler method. The parser uses the instance to report basic document-related events like the start and end of elements and character data. Internet Technologies

Some methods from the Content. Handler Interface void characters(…) Receive notification of character data.

Some methods from the Content. Handler Interface void characters(…) Receive notification of character data. void end. Document(…) Receive notification of the end of a document. void end. Element(…) Receive notification of the end of an element. void start. Document(…) Receive notification of the beginning of a document. void start. Element(…) Receive notification of the beginning of an element. Internet Technologies

public interface Error. Handler Basic interface for SAX error handlers. If a SAX application

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. The parser will then report all errors and warnings through this interface. For XML processing errors, a SAX driver must use this interface instead of throwing an exception: it is up to the application to decide 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. Internet Technologies

public interface Error. Handler Some methods are: void error(SAXParse. Exception exception) Receive notification of

public interface Error. Handler Some methods are: void error(SAXParse. Exception exception) Receive notification of a recoverable error. void fatal. Error(SAXParse. Exception exception) Receive notification of a non-recoverable error. void warning(SAXParse. Exception exception) Receive notification of a warning. Internet Technologies

public class Default. Handler extends java. lang. Object implements Entity. Resolver, DTDHandler, Content. Handler,

public class Default. Handler extends java. lang. Object implements Entity. Resolver, DTDHandler, Content. Handler, Error. Handler Default base class for handlers. This class implements the default behaviour four SAX interfaces: Entity. Resolver, DTDHandler, Content. Handler, and Error. Handler. Internet Technologies

Fixed. Float. Swap. dtd <? xml version="1. 0" encoding="utf-8"? > <!ELEMENT Fixed. Float. Swap

Fixed. Float. Swap. dtd <? xml version="1. 0" encoding="utf-8"? > <!ELEMENT Fixed. Float. Swap ( Bank, Notional, Fixed_Rate, Num. Years, Num. Payments ) > <!ELEMENT Bank (#PCDATA)> <!ELEMENT Notional (#PCDATA)> <!ATTLIST Notional currency (dollars | pounds) #REQUIRED> <!ELEMENT Fixed_Rate (#PCDATA) > <!ELEMENT Num. Years (#PCDATA) > <!ELEMENT Num. Payments (#PCDATA) > Input DTD Internet Technologies

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" [ <!ENTITY bankname "Pittsburgh National Corporation"> ] > <Fixed. Float. Swap> <Bank>&bankname; </Bank> <Notional currency = "pounds">100</Notional> <Fixed_Rate>5</Fixed_Rate> <Num. Years>3</Num. Years> <Num. Payments>6</Num. Payments> Input XML </Fixed. Float. Swap> Internet Technologies

Processing // Notify. Str. java // Adapted from XML and Java by Maruyama, Tamura

Processing // Notify. Str. java // Adapted from XML and Java by Maruyama, Tamura and // Uramoto import java. io. *; import org. xml. sax. helpers. *; import javax. xml. parsers. *; public class Notify. Str extends Default. Handler { Internet Technologies

public static void main (String argv []) throws IOException, SAXException { if (argv. length

public static void main (String argv []) throws IOException, SAXException { if (argv. length != 1) { System. err. println ("Usage: java Notify. Str filename. xml"); System. exit (1); } XMLReader reader = XMLReader. Factory. create. XMLReader( "org. apache. xerces. parsers. SAXParser"); Input. Source input. Source = new Input. Source(argv[0]); reader. set. Content. Handler(new Notify. Str()); reader. parse(input. Source); System. exit (0); } Internet Technologies

public Notify. Str() {} public void start. Document() throws SAXException { System. out. println("start.

public Notify. Str() {} public void start. Document() throws SAXException { System. out. println("start. Document called: "); } public void end. Document() throws SAXException { System. out. println("end. Document called: "); } Internet Technologies

public void start. Element(String namespace. URI, String local. Name, String q. Name, Attributes a.

public void start. Element(String namespace. URI, String local. Name, String q. Name, Attributes a. Map) throws SAXException { System. out. println("start. Element called: element name =" + local. Name); // examine the attributes for(int i = 0; i < a. Map. get. Length(); i++) { String att. Name = a. Map. get. Local. Name(i); String type = a. Map. get. Type(i); String value = a. Map. get. Value(i); System. out. println(" attribute name = " + att. Name + " type = " + type + " value = " + value); } } Internet Technologies

public void characters(char[] ch, int start, int length) throws SAXException { // build String

public void characters(char[] ch, int start, int length) throws SAXException { // build String from char array String data. Found = new String(ch, start, length); System. out. println("characters called: " + data. Found); } } Internet Technologies

C: Mc. Carthywww95 -733examplessax>java Notify. Str Fixed. Float. Swap. xml start. Document called: start.

C: Mc. Carthywww95 -733examplessax>java Notify. Str Fixed. Float. Swap. xml start. Document called: start. Element called: element name =Fixed. Float. Swap start. Element called: element name =Bank characters called: Pittsburgh National Corporation start. Element called: element name =Notional attribute name = currency type = dollars|pounds value = pounds characters called: 100 start. Element called: element name =Fixed_Rate characters called: 5 start. Element called: element name =Num. Years characters called: 3 start. Element called: element name =Num. Payments characters called: 6 Output end. Document called: Internet Technologies

Accessing the swap from the internet <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE Fixed.

Accessing the swap from the internet <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE Fixed. Float. Swap [ <!ENTITY bankname "Pittsburgh National Corporation"> ] > <Fixed. Float. Swap> <Bank>&bankname; </Bank> <Notional currency = "pounds">100</Notional> <Fixed_Rate>5</Fixed_Rate> <Num. Years>3</Num. Years> <Num. Payments>6</Num. Payments> </Fixed. Float. Swap> Saved under Internet webapps/sax/fpml/Fixed. Float. Swap. xml Technologies

The Deployment Descriptor <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE web-app PUBLIC "-//Sun Microsystems,

The Deployment Descriptor <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc. //DTD Web Application 2. 2//EN" "http: //java. sun. com/j 2 ee/dtds/web-app_2. 2. dtd"> <web-app> webapps/sax/WEB-INF/web. xml <servlet> <servlet-name>Sax. Example</servlet-name> <servlet-class>Get. XML</servlet-class> </servlet> <servlet-mapping> <servlet-name>Sax. Example</servlet-name> <url-pattern>/Get. XML/*</url-pattern> </servlet-mapping> </web-app> Internet Technologies

// This servlet file is stored under Tomcat in // webapps/sax/WEB-INF/classes/Get. XML. java //

// This servlet file is stored under Tomcat in // webapps/sax/WEB-INF/classes/Get. XML. java // This servlet returns a user selected xml file from // webapps/sax/fpml directory // and returns it as a string to the client. // Servlet import java. io. *; import java. util. *; import javax. servlet. http. *; public class Get. XML extends Http. Servlet { Internet Technologies

public void do. Get(Http. Servlet. Request req, Http. Servlet. Response res) throws Servlet. Exception,

public void do. Get(Http. Servlet. Request req, Http. Servlet. Response res) throws Servlet. Exception, IOException { System. out. println("do. Get called with " + req. get. Path. Info()); String the. Data = ""; String extra. Path = req. get. Path. Info(); extra. Path = extra. Path. substring(1); // read the file try { // open file and create a Data. Input. Stream File. Input. Stream the. File = new File. Input. Stream( "D: \jakarta-tomcat-4. 0. 1\webapps\sax\fpml\“ +extra. Path); Internet Technologies

Input. Stream. Reader is = new Input. Stream. Reader(the. File); Buffered. Reader br =

Input. Stream. Reader is = new Input. Stream. Reader(the. File); Buffered. Reader br = new Buffered. Reader(is); // read the file into the string the. Data String this. Line; while((this. Line = br. read. Line()) != null) { the. Data += this. Line + "n"; } } catch(Exception e) { System. err. println("Error " + e); } Internet Technologies

Print. Writer out = res. get. Writer(); out. write(the. Data); System. out. println("Wrote document

Print. Writer out = res. get. Writer(); out. write(the. Data); System. out. println("Wrote document to client"); //System. out. println(the. Data); out. close(); } } Internet Technologies

// Tomcat. Notify. Str. java // Adapted from XML and Java by Maruyama, Tamura

// Tomcat. Notify. Str. java // Adapted from XML and Java by Maruyama, Tamura and Uramoto import java. io. *; import org. xml. sax. helpers. *; import javax. xml. parsers. *; // Client public class Tomcat. Notify. Str extends Default. Handler { public static void main (String argv []) throws IOException, SAXException { if (argv. length != 1) { System. err. println ("Usage: java Notify. Str filename. xml"); System. exit (1); Internet Technologies }

XMLReader reader = XMLReader. Factory. create. XMLReader( "org. apache. xerces. parsers. SAXParser"); String server.

XMLReader reader = XMLReader. Factory. create. XMLReader( "org. apache. xerces. parsers. SAXParser"); String server. String = "http: //localhost: 8080/sax/Get. XML/"; String file. Name = argv[0]; Input. Source input. Source = new Input. Source(server. String + file. Name); reader. set. Content. Handler(new Tomcat. Notify. Str()); reader. parse(input. Source); System. exit (0); } Internet Technologies

public Tomcat. Notify. Str() {} public void start. Document() throws SAXException { System. out.

public Tomcat. Notify. Str() {} public void start. Document() throws SAXException { System. out. println("start. Document called: "); } public void end. Document() throws SAXException { System. out. println("end. Document called: "); } Internet Technologies

public void start. Element(String namespace. URI, String local. Name, String q. Name, Attributes a.

public void start. Element(String namespace. URI, String local. Name, String q. Name, Attributes a. Map) throws SAXException { System. out. println("start. Element called: element name =" + local. Name); // examine the attributes for(int i = 0; i < a. Map. get. Length(); i++) { String att. Name = a. Map. get. Local. Name(i); String type = a. Map. get. Type(i); String value = a. Map. get. Value(i); System. out. println(" attribute name = " + att. Name + " type = " + type + " value = " + value); } } Internet Technologies

public void characters(char[] ch, int start, int length) throws SAXException { // build String

public void characters(char[] ch, int start, int length) throws SAXException { // build String from char array String data. Found = new String(ch, start, length); System. out. println("characters called: " + data. Found); } } Internet Technologies

Being served by the servlet <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE Fixed. Float.

Being served by the servlet <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE Fixed. Float. Swap [ <!ENTITY bankname "Pittsburgh National Corporation"> ] > <Fixed. Float. Swap> <Bank>&bankname; </Bank> <Notional currency = "pounds">100</Notional> <Fixed_Rate>5</Fixed_Rate> <Num. Years>3</Num. Years> <Num. Payments>6</Num. Payments> </Fixed. Float. Swap> Internet Technologies

C: Mc. Carthywww95 -733examplessax>java Tomcat. Notify. Str Fixed. Float. Swap. xml start. Document called:

C: Mc. Carthywww95 -733examplessax>java Tomcat. Notify. Str Fixed. Float. Swap. xml start. Document called: start. Element called: element name =Fixed. Float. Swap characters called: start. Element called: element name =Bank characters called: Pittsburgh National Corporation characters called: start. Element called: element name =Notional attribute name = currency type = CDATA value = pounds characters called: 100 characters called: start. Element called: element name =Fixed_Rate characters called: 5 characters called: start. Element called: element name =Num. Years characters called: 3 characters called: start. Element called: element name =Num. Payments characters called: 6 characters called: end. Document called: Internet Technologies Output

Let’s Add Back the DTD… <? xml version="1. 0" encoding="utf-8"? > <!ELEMENT Fixed. Float.

Let’s Add Back the DTD… <? xml version="1. 0" encoding="utf-8"? > <!ELEMENT Fixed. Float. Swap ( Bank, Notional, Fixed_Rate, Num. Years, Num. Payments ) > <!ELEMENT Bank (#PCDATA)> <!ELEMENT Notional (#PCDATA)> <!ATTLIST Notional currency (dollars | pounds) #REQUIRED> <!ELEMENT Fixed_Rate (#PCDATA) > <!ELEMENT Num. Years (#PCDATA) > <!ELEMENT Num. Payments (#PCDATA) > Internet Technologies

And reference the DTD in the XML <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE

And reference the DTD in the XML <? xml version="1. 0" encoding="UTF-8"? > <!DOCTYPE Fixed. Float. Swap SYSTEM "Fixed. Float. Swap. dtd" [ <!ENTITY bankname "Pittsburgh National Corporation"> ] > <Fixed. Float. Swap> <Bank>&bankname; </Bank> <Notional currency = "pounds">100</Notional> <Fixed_Rate>5</Fixed_Rate> <Num. Years>3</Num. Years> <Num. Payments>6</Num. Payments> </Fixed. Float. Swap> Internet Technologies

We get new output C: Mc. Carthywww95 -733examplessax>java Tomcat. Notify. Str Fixed. Float. Swap.

We get new output C: Mc. Carthywww95 -733examplessax>java Tomcat. Notify. Str Fixed. Float. Swap. xml start. Document called: start. Element called: element name =Fixed. Float. Swap start. Element called: element name =Bank characters called: Pittsburgh National Corporation start. Element called: element name =Notional attribute name = currency type = dollars|pounds value = pounds characters called: 100 start. Element called: element name =Fixed_Rate characters called: 5 start. Element called: element name =Num. Years How many times did we characters called: 3 visit the servlet? start. Element called: element name =Num. Payments Twice. Once for the xml characters called: 6 end. Document called: and a second time for the DTD. Internet Technologies

We don’t have to go through a servlet…Tomcat can send the files String server.

We don’t have to go through a servlet…Tomcat can send the files String server. String = "http: //localhost: 8080/sax/fpml/"; String file. Name = argv[0]; Input. Source is = new Input. Source(server. String + file. Name); But the servlet illustrates that the XML data can be generated dynamically. Internet Technologies

The Input. Source Class The SAX and DOM parsers need XML input. The “output”

The Input. Source Class The SAX and DOM parsers need XML input. The “output” produced by these parsers amounts to a series of method calls (SAX) or an application programmer interface to the tree (DOM). An Input. Source object can be used to provided input to the parser. Tree Input. Surce SAX or DOM So, how do we build an Input. Source object? Internet Technologies Events application

The Input. Source Class Some Input. Source constructors: Input. Source(String path. To. File); Input.

The Input. Source Class Some Input. Source constructors: Input. Source(String path. To. File); Input. Source(Input. Stream byte. Stream); Input. Stream(Reader character. Stream); For example: String text = “<a>some xml</a>”; String. Reader sr = new String. Reader(text); Input. Source is = new Input. Source(sr); : my. Parser. parse(is); Internet Technologies

But what about the DTD? public interface Entity. Resolver Basic interface for resolving entities.

But what about the DTD? public interface Entity. Resolver Basic interface for resolving entities. If a SAX application needs to implement customized handling for external entities, it must implement this interface and register an instance with the SAX parser using the parser's set. Entity. Resolver method. The parser will then allow the application to intercept any external entities (including the external DTD subset and external parameter entities, if any) before including them. Internet Technologies

Entity. Resolver public Input. Source resolve. Entity(String public. Id, String system. Id) { //

Entity. Resolver public Input. Source resolve. Entity(String public. Id, String system. Id) { // Add this method to the client above. The system. Id String // holds the path to the dtd as specified in the xml document. // We may now access the dtd from a servlet and return an // Input. Stream or return null and let the parser resolve the // external entity. System. out. println("Attempting to resolve" + "Public id : " + public. Id + "System id : " + system. Id); return null; } Internet Technologies

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

Processing XML with 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 Internet Technologies

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. Internet Technologies

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> Internet Technologies

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 Internet Technologies 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> Internet Technologies

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 Technologies All of these nodes. Internet implement the Node interface 6

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

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

Some DOM Documentation from Java. Soft Internet Technologies

Some DOM Documentation from Java. Soft Internet Technologies

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. Internet Technologies

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… Internet Technologies

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 Internet Technologies

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 … Internet Technologies

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. Internet Technologies

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 Internet Technologies

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. Internet Technologies

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. Internet Technologies

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. Internet Technologies

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. Internet Technologies

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. Internet Technologies

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. Internet Technologies

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. Internet Technologies

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. Internet Technologies

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. . Internet Technologies

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. Internet Technologies

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. Internet Technologies 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. Internet Technologies

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". Internet Technologies

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 all the matched Elements. Internet containing Technologies

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> Internet Technologies

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 Internet Technologies 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; Internet Technologies 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(); Internet Technologies

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()); } Internet Technologies

} 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); } } Internet Technologies

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> Internet Technologies

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

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

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

Another DOM Example A Java Program that reads Fixed. Float. Swap. xml from Tomcat and performs validation against the server based DTD. The program then displays the DOM tree. Internet Technologies

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); Internet Technologies

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; Technologies Internet }

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). Internet Technologies

Input. Source is = new Input. Source("http: //mccarthy. heinz. cmu. edu: 8080/fpml/Fixed. Float. Swap.

Input. Source is = new Input. Source("http: //mccarthy. heinz. cmu. edu: 8080/fpml/Fixed. Float. Swap. 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 webapps/ROOT/fpml Tomcat’s port. } A single input source for an XML entity. Internet Technologies

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); } } Internet Technologies

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

A 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); } } Internet Technologies

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 Internet Technologies 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; Internet Technologies

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(); } } } Internet Technologies

Output C: Mc. CarthywwwFinancial EngineeringFixed. Float. Swap>java Simulator 6 No Problems found #document. .

Output C: Mc. CarthywwwFinancial EngineeringFixed. Float. Swap>java Simulator 6 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 Internet Technologies #text. . . Text Node

Building a DOM Tree From Scratch My. Grade. Book. xml <? xml version="1. 0"

Building a DOM Tree From Scratch My. Grade. Book. xml <? xml version="1. 0" encoding="UTF-8"? > <Grade. Book> <Student> <Score>100</Score> </Student> </Grade. Book> Internet Technologies Let’s create this file from within a java program.

GOAL C: Mc. Carthywww95 -733examplesdom>java Dom. Example C: Mc. Carthywww95 -733examplesdom>type My. Grade. Book.

GOAL C: Mc. Carthywww95 -733examplesdom>java Dom. Example C: Mc. Carthywww95 -733examplesdom>type My. Grade. Book. xml <? xml version="1. 0"? > <Grade. Book><Student><Score>100</Score></Student></Grade. Book> Internet Technologies

// Dom. Example. java // Building an xml document from scratch import java. io.

// Dom. Example. java // Building an xml document from scratch import java. io. *; import javax. xml. parsers. Document. Builder. Factory; import javax. xml. parsers. Document. Builder; import javax. xml. parsers. Parser. Configuration. Exception; import org. w 3 c. dom. *; import org. apache. xml. serialize. XMLSerializer; // not standard import org. apache. xml. serialize. Output. Format; // not standard Internet Technologies

public class Dom. Example { private Document document; public Dom. Example () { Document.

public class Dom. Example { private Document document; public Dom. Example () { Document. Builder. Factory factory = Document. Builder. Factory. new. Instance(); try { Document. Builder builder = factory. new. Document. Builder(); document = builder. new. Document(); } catch (Throwable t) { t. print. Stack. Trace (); Internet Technologies }

// Ask the Document object for various types // of nodes and // add

// Ask the Document object for various types // of nodes and // add them to the tree. Element root = document. create. Element("Grade. Book"); document. append. Child(root); Element student = document. create. Element("Student"); root. append. Child(student); Element score = document. create. Element("Score"); student. append. Child(score); Text value = document. create. Text. Node("100"); score. append. Child(value); Internet Technologies

// Write the Document to disk using Xerces. try { File. Output. Stream fos

// Write the Document to disk using Xerces. try { File. Output. Stream fos = new File. Output. Stream( "My. Grade. Book. xml"); XMLSerializer xml. Writer = new XMLSerializer(fos, null); xml. Writer. serialize(document); } catch(IOException ioe) { ioe. print. Stack. Trace(); } } Internet Technologies

public static void main(String a[]) { Dom. Example tree = new Dom. Example(); }

public static void main(String a[]) { Dom. Example tree = new Dom. Example(); } } Internet Technologies