Java and XML DOM and SAX Some of

  • Slides: 115
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… OCT 1

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) OCT 2

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> OCT 3

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) > OCT 4

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 OCT 5

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) OCT 6

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 OCT 7

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); } OCT 8

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 OCT 9

Some of the XML API’s in JDK 1. 4 OCT 10

Some of the XML API’s in JDK 1. 4 OCT 10

There are more in JWSDP OCT 11

There are more in JWSDP OCT 11

Important SAX interfaces and classes (JDK 1. 4) class Input. Source -- A single

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

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. OCT 13

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 OCT 14

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. OCT 15

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. OCT 16

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. OCT 17

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. OCT 18

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 behavior four SAX interfaces: Entity. Resolver, DTDHandler, Content. Handler, and Error. Handler. OCT 19

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 OCT 20

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> OCT 21

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 { OCT 22

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); } OCT 23

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: "); } OCT 24

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); <pre: Local. Part> // examine the attributes Qualified Name 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); } } OCT 25

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); } } OCT 26

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: OCT 27

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 webapps/sax/fpml/Fixed. Float. Swap. xml OCT 28

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> OCT 29

// 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 { OCT 30

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); OCT 31

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); } OCT 32

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(); } } OCT 33

// 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); OCT 34 }

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); } OCT 35

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: "); } OCT 36

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); } } OCT 37

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); } } OCT 38

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> OCT 39

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: Output 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: OCT 40

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) > OCT 41

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> OCT 42

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 OCT DTD. 43

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. OCT 44

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 provide input to the parser. Tree Input. Surce SAX or DOM So, how do we build an Input. Source object? OCT Events application 45

Pass The Input. Source Class to the Parser Some Input. Source constructors: Input. Source(String

Pass The Input. Source Class to the Parser Some Input. Source constructors: Input. Source(String path. To. File. Or. AURL); Input. Source(Input. Stream byte. Stream); // bytes Input. Stream(Reader character. Stream); // Characters 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); OCT 46

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 OCTthem. 47

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 could now build an Input. Stream object from a different // system. ID and return this new Input. Stream. Or simply 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; } OCT 48

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

Processing XML with DOM • The following examples were tested using Sun’s JAXP (JDK 1. 4) OCT 49

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. OCT 50

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> OCT 51

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 OCT element Little cat B Little cat D element Little Cat C text I am little cat C 52

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> OCT 53

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 OCT All of these nodes implement the Node interface 6 54

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 OCT 55

Some DOM Documentation from Java. Soft OCT 56

Some DOM Documentation from Java. Soft OCT 56

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. OCT 57

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… OCT 58

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 OCT 59

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 … OCT 60

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. OCT 61

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 OCT 62

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. OCT 63

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. OCT 64

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. OCT 65

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. OCT 66

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. OCT 67

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. OCT 68

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. OCT 69

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. OCT 70

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. . OCT 71

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. OCT 72

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. OCT public void set. Data(String data) is also available. 73

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. OCT 74

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". OCT 75

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. OCT 76

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> OCT 77

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 OCT 6 78

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; OCT Process a local 79 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(); OCT 80

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

doc = doc. Builder. parse(new File(argv[0])); Element top = doc. get. Document. Element(); 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()); } OCT 81

} 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); } } OCT 82

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> OCT 83

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

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

Read-Process-Write XML My. Grade. Book. xml <? xml version="1. 0"? > <Grade. Book> <Student>

Read-Process-Write XML My. Grade. Book. xml <? xml version="1. 0"? > <Grade. Book> <Student> <Score>100</Score> </Student> </Grade. Book> OCT 85

Change. Grade. java import java. io. File; import org. w 3 c. dom. *;

Change. Grade. java 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; import org. apache. xml. serialize. Output. Format; import org. apache. xml. serialize. XMLSerializer; import java. io. Byte. Array. Output. Stream; OCT 86

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

public class Change. Grade { public static void main(String argv[]) { Document doc; if(argv. length != 2 ) { System. err. println( "usage: java Change. Gradebook. Name new. Score"); System. exit(1); } try { Document. Builder. Factory doc. Builder. Factory = Document. Builder. Factory. new. Instance(); Document. Builder doc. Builder = OCT 87 doc. Builder. Factory. new. Document. Builder();

// read and parse the document and build the Dom tree doc = doc.

// read and parse the document and build the Dom tree doc = doc. Builder. parse(new File(argv[0])); // get the score String new. Score = (new Integer(argv[1])). to. String(); // get document element Element top = doc. get. Document. Element(); // get and change score element Node. List another. List = top. get. Elements. By. Tag. Name("Score"); Element score = (Element)another. List. item(0); Text t = (Text)score. get. First. Child(); t. set. Node. Value(new. Score); OCT 88

// write the document to a file Output. Format formatter = new Output. Format();

// write the document to a file Output. Format formatter = new Output. Format(); XMLSerializer serializer = new XMLSerializer(System. out, formatter serializer. serialize(doc); // write the document to a byte array Byte. Array. Output. Stream baos = new Byte. Array. Output. Stream(); serializer = new XMLSerializer(baos, formatter); serializer. serialize(doc); // fill an array of bytes byte[] bytes = baos. to. Byte. Array(); OCT 89

// convert to string String s = baos. to. String(); System. out. println("As a

// convert to string String s = baos. to. String(); System. out. println("As a String"); System. out. println(s); } 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); } OCT } 90

D: Mc. Carthywww95 -733examplesdom> java Change. Grade My. Grade. Book. xml 34 <? xml

D: Mc. Carthywww95 -733examplesdom> java Change. Grade My. Grade. Book. xml 34 <? xml version="1. 0" encoding="UTF-8"? > <Grade. Book> <Student> <Score>34</Score> </Student> </Grade. Book> As a String <? xml version="1. 0" encoding="UTF-8"? > <Grade. Book> <Student> <Score>34</Score> </Student> </Grade. Book> OCT 91

Using the Input. Source class D: Mc. Carthywww95 -733examplesdominternetexample>tree /f Directory PATH listing Volume

Using the Input. Source class D: Mc. Carthywww95 -733examplesdominternetexample>tree /f Directory PATH listing Volume serial number is 0012 FC 94 486 D: D 392 D: . │ build. properties │ build. xml ├───build │ │ My. Grade. Book. xml │ │ │ └───WEB-INF │ ├───classes │ └───lib ├───src └───web OCT 92 My. Grade. Book. xml

Using the Input. Source Class import java. io. File; import org. w 3 c.

Using the Input. Source Class 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; import org. apache. xml. serialize. Output. Format; import org. apache. xml. serialize. XMLSerializer; import java. io. Byte. Array. Output. Stream; import org. xml. sax. Input. Source; OCT 93

public class Read. Grades. From. Internet { public static void main(String argv[]) { Document

public class Read. Grades. From. Internet { public static void main(String argv[]) { Document doc; try { Document. Builder. Factory doc. Builder. Factory = Document. Builder. Factory. new. Instance(); Document. Builder doc. Builder = doc. Builder. Factory. new. Document. Builder(); Input. Source input. Source = new Input. Source( "http: //localhost: 8080/Grades/My. Grade. Book. xml"); // read and parse the document and build the Dom tree doc = doc. Builder. parse(input. Source); OCT 94

// write the document to a file Output. Format formatter = new Output. Format();

// write the document to a file Output. Format formatter = new Output. Format(); XMLSerializer serializer = new XMLSerializer( System. out, formatter); serializer. serialize(doc); } 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) { OCT t. print. Stack. Trace(); } System. exit(0); }} 95

D: Mc. Carthywww95 -733examplesdom> java Read. Grades. From. Internet <? xml version="1. 0" encoding="UTF-8"?

D: Mc. Carthywww95 -733examplesdom> java Read. Grades. From. Internet <? xml version="1. 0" encoding="UTF-8"? > <Grade. Book> <Student> <Score>100</Score> </Student> </Grade. Book> OCT 96

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

Example - 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); } } OCT 97

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 OCT 6 98

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; OCT 99

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(); } } } OCT 100

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 OCT #text. . . Text Node 101

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> Let’s create this file from within a java program. OCT 102

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> OCT 103

// 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 OCT 104

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 (); OCT } 105

// 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); OCT 106

// 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(); } } OCT 107

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

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

A SAX Example Using a Factory // Simple. SAX. java import java. io. *;

A SAX Example Using a Factory // Simple. SAX. java import java. io. *; import org. xml. sax. *; import javax. xml. parsers. SAXParser. Factory; import javax. xml. parsers. Parser. Configuration. Exception; import javax. xml. parsers. SAXParser; import org. xml. sax. helpers. Default. Handler; OCT 109

public class Simple. SAX extends Default. Handler { public static void main (String argv

public class Simple. SAX extends Default. Handler { public static void main (String argv []) { if (argv. length != 1) { System. err. println ( // enter a local or distant file name "Usage: java Simple. SAX XMLFile. URL"); System. exit (1); } OCT 110

String file. Name. URL = argv[0]; System. out. println("Will attempt to process " +

String file. Name. URL = argv[0]; System. out. println("Will attempt to process " + file. Name. URL); Input. Source is = new Input. Source(file. Name. URL); Simple. SAX my. Handler = new Simple. SAX(is); System. out. println("Processed " + my. Handler. get. Counter()); System. exit (0); } OCT 111

SAXParser. Factory factory = SAXParser. Factory. new. Instance(); int counter = 0; public Simple.

SAXParser. Factory factory = SAXParser. Factory. new. Instance(); int counter = 0; public Simple. SAX(Input. Source is) { factory. set. Validating(false); try { SAXParser sax. Parser = factory. new. SAXParser(); sax. Parser. parse( is, this); } catch (Throwable t) { System. out. println("Error in constructor"); t. print. Stack. Trace (); } } OCT 112

public int get. Counter() { return counter; } public void start. Document() throws SAXException

public int get. Counter() { return counter; } public void start. Document() throws SAXException { System. out. println("start. Document called: "); } public void end. Document() throws SAXException { System. out. println("end. Document called: "); } OCT 113

public void start. Element(String uri, String local. Name, String q. Name, Attributes attributes) throws

public void start. Element(String uri, String local. Name, String q. Name, Attributes attributes) throws SAXException { counter++; System. out. println("start. Element called: element name =" + q. Name); } public void end. Element(String name) throws SAXException { System. out. println("end. Element is called: " + name); } 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); } OCT 114

public void error(SAXParse. Exception e) throws SAXException { System. out. println("Parsing error"); System. out.

public void error(SAXParse. Exception e) throws SAXException { System. out. println("Parsing error"); System. out. println(e. to. String()); } } OCT 115