OCT1 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”

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Web architectures – Laurea Specialistica in Informatica – Università di Trento Java XML parsing.
Advertisements

Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 22. Oktober 2009.
XML Parsers By Chongbing Liu. XML Parsers  What is a XML parser?  DOM and SAX parser API  Xerces-J parsers overview  Work with XML parsers (example)
Internet Technologies1 XML Messaging A PowerWarning application using servlets and SAX The PowerWarning Application is from “XML and Java” by Maruyama,
Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.
Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course.
1 SAX and more… CS , Spring 2008/9. 2 SAX Parser SAX = Simple API for XML XML is read sequentially When a parsing event happens, the parser invokes.
1 The Simple API for XML (SAX) Part I ©Copyright These slides are based on material from the upcoming book, “XML and Bioinformatics” (Springer-
14-Jun-15 DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard SAX is an ad-hoc.
Parsing XML into programming languages JAXP, DOM, SAX, JDOM/DOM4J, Xerces, Xalan, JAXB.
Financial Engineering Project Course. Lecture 3 Object Oriented Design Inheritance Abstract Base Classes Polymorphism Using XML to represent the swap.
Week 5 Basic SAX Example From Chapter 5 of XML and Java Working with XML SAX Filters as described in Chapter 5.
21-Jun-15 SAX (Abbreviated). 2 XML Parsers SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard.
Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course.
26-Jun-15 SAX. SAX and DOM SAX and DOM are standards for XML parsers--program APIs to read and interpret XML files DOM is a W3C standard SAX is an ad-hoc.
Internet Technologies1 More XML Schema The main source for these slides is “The XML Companion” by Bradley Other resources:
Internet Technologies 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.
Processing XML Processing XML using XSLT Processing XML documents with Java (DOM) Next week -- Processing XML documents with Java (SAX)
Internet Technologies1 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.
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
Processing XML Part II Parser Operations with DOM and SAX overview XML Validation with examples Processing XML with SAX (locally and on the internet)
17 Apr 2002 XML Programming: SAX Andy Clark. SAX Design Premise Generic method of creating XML parser, parsing documents, and receiving document information.
1 Processing XML with Java Dr. Praveen Madiraju Modified from Dr.Sagiv ’ s slides.
Processing of structured documents Spring 2003, Part 5 Helena Ahonen-Myka.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
17 Apr 2002 XML Programming: JAXP Andy Clark. Java API for XML Processing Standard Java API for loading, creating, accessing, and transforming XML documents.
The Joy of SAX (and DOM, and JDOM…) Bill MacCartney 11 October 2004.
SDPL 2003Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
1 XML at a neighborhood university near you Innovation 2005 September 16, 2005 Kwok-Bun Yue University of Houston-Clear Lake.
SAX Parsing Presented by Clifford Lemoine CSC 436 Compiler Design.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
1 Java and XML Modified from presentation by: Barry Burd Drew University Portions © 2002 Hungry Minds, Inc.
SDPL 2002Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
SAX. What is SAX SAX 1.0 was released on May 11, SAX is a common, event-based API for parsing XML documents Primarily a Java API but there implementations.
Beginning XML 4th Edition. Chapter 12: Simple API for XML (SAX)
XML Processing in Java. Required tools Sun JDK 1.4, e.g.: JAXP (part of Java Web Services Developer Pack, already in Sun.
Java API for XML Processing (JAXP) Dr. Rebhi S. Baraka Advanced Topics in Information Technology (SICT 4310) Department of Computer.
Sheet 1XML Technology in E-Commerce 2001Lecture 3 XML Technology in E-Commerce Lecture 3 DOM and SAX.
1 Processing XML with Java Modified from Dr.Sagiv ’ s slides.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
Document Object Model DOM. Agenda l Introduction to DOM l Java API for XML Parsing (JAXP) l Installation and setup l Steps for DOM parsing l Example –Representing.
SNU OOPSLA Lab. DOM/SAX Applications The ubiquitous XML(9) © copyright 2001 SNU OOPSLA Lab.
Java and XML. What is XML XML stands for eXtensible Markup Language. A markup language is used to provide information about a document. Tags are added.
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
© Marty Hall, Larry Brown Web core programming 1 Simple API for XML SAX.
XML and SAX (A quick overview) ● What is XML? ● What are SAX and DOM? ● Using SAX.
When we create.rtf document apart from saving the actual info the tool saves additional info like start of a paragraph, bold, size of the font.. Etc. This.
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
Java Web 应用开发: J2EE 和 Tomcat 蔡 剑, Ph.D.. 本讲内容 Web 层技术 (III) Custom Tags JSP and XML JSTL.
SDPL 20063: XML Processor Interfaces1 3. XML Processor APIs n How can (Java) applications manipulate structured (XML) documents? –An overview of XML processor.
Simple API for XML (SAX) Aug’10 – Dec ’10. Introduction to SAX Simple API for XML or SAX was developed as a standardized way to parse an XML document.
7-Mar-16 Simple API XML.  SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files  DOM is a W3C standard  SAX is an.
13-Mar-16 DOM. 2 Difference between SAX and DOM DOM reads the entire XML document into memory and stores it as a tree data structure SAX reads the XML.
SDPL 2001Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How applications can manipulate structured documents? –An overview of document parser.
USING ANDROID WITH THE DOM. Slide 2 Lecture Summary DOM concepts SAX vs DOM parsers Parsing HTTP results The Android DOM implementation.
1 Introduction SAX. Objectives 2  Simple API for XML  Parsing an XML Document  Parsing Contents  Parsing Attributes  Processing Instructions  Skipped.
21-Jun-16 Document Object Model DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C.
Java API for XML Processing
Simple API for XML SAX. Agenda l Introduction to SAX l Installation and setup l Steps for SAX parsing l Defining a content handler l Examples Printing.
Parsing with SAX using Java Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
XML. Contents  Parsing an XML Document  Validating XML Documents.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
Parsing XML into programming languages
Java API for XML Processing
DOM 8-Dec-18.
DOM 24-Feb-19.
SAX2 29-Jul-19.
WaysInJavaToParseXML
Presentation transcript:

OCT1 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…

OCT2 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)

OCT3 FixedFloatSwap.xml

OCT4 FixedFloatSwap.dtd <!ELEMENT FixedFloatSwap (Notional, Fixed_Rate, NumYears, NumPayments ) >

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

OCT6 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 FpML to WML)

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

OCT8 Operation of an Event Based Parser Event-Based Parser Application Logic Valid XML DTD XML Document public void startDocument () public void endDocument () public void startElement (…)) public void endElement (…) public void characters (…)) public void error(SAXParseException e) throws SAXException { System.out.println("\n\n--Invalid document ---" + e); }

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

OCT10 Some of the XML API’s in JDK1.4

OCT11 There are more in JWSDP

OCT12 Important SAX interfaces and classes (JDK1.4) class InputSource -- A single input source for an XML entity interface XMLReader -- defines parser behavior (implemented by Xerces’ SAXParser and others) Four core SAX2 handler interfaces: EntityResolver DTDHandler ContentHandler ErrorHandler Implemented by class DefaultHandler

OCT13 Processing XML with SAX interface XMLReader -- defines parser behavior (implemented by Xerces’ SAXParser) XMLReader is the interface that an XML parser's SAX2 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.

OCT14 Processing XML with SAX We will look at the following interfaces and classes and then study an example interface ContentHandler -- reports on document events interface ErrorHandler – reports on validity errors class DefaultHandler – implements both of the above plus two others

OCT15 public interface ContentHandler 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 setContentHandler method. The parser uses the instance to report basic document-related events like the start and end of elements and character data.

OCT16 void characters(…) Receive notification of character data. void endDocument(…) Receive notification of the end of a document. void endElement(…) Receive notification of the end of an element. void startDocument(…) Receive notification of the beginning of a document. void startElement(…) Receive notification of the beginning of an element. Some methods from the ContentHandler Interface

OCT17 public interface ErrorHandler 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 fatalError. fatalError

OCT18 public interface ErrorHandler Some methods are: void error(SAXParseException exception) Receive notification of a recoverable error. void fatalError(SAXParseException exception) Receive notification of a non-recoverable error. void warning(SAXParseException exception) Receive notification of a warning.

OCT19 public class DefaultHandler extends java.lang.Object implements EntityResolver, DTDHandler, ContentHandler, ErrorHandler Default base class for handlers. This class implements the default behavior for four SAX interfaces: EntityResolver, DTDHandler, ContentHandler, and ErrorHandler.

OCT20 <!ELEMENT FixedFloatSwap ( Bank, Notional, Fixed_Rate, NumYears, NumPayments ) > FixedFloatSwap.dtd Input DTD

OCT21 <!DOCTYPE FixedFloatSwap SYSTEM "FixedFloatSwap.dtd" [ ] > &bankname; FixedFloatSwap.xml Input XML

OCT22 Processing // NotifyStr.java // Adapted from XML and Java by Maruyama, Tamura and // Uramoto import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import javax.xml.parsers.*; public class NotifyStr extends DefaultHandler {

OCT23 public static void main (String argv []) throws IOException, SAXException { if (argv.length != 1) { System.err.println ("Usage: java NotifyStr filename.xml"); System.exit (1); } XMLReader reader = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser"); InputSource inputSource = new InputSource(argv[0]); reader.setContentHandler(new NotifyStr()); reader.parse(inputSource); System.exit (0); }

OCT24 public NotifyStr() {} public void startDocument() throws SAXException { System.out.println("startDocument called:"); } public void endDocument() throws SAXException { System.out.println("endDocument called:"); }

OCT25 public void startElement(String namespaceURI, String localName, String qName, Attributes aMap) throws SAXException { System.out.println("startElement called: element name =" + localName); // examine the attributes for(int i = 0; i < aMap.getLength(); i++) { String attName = aMap.getLocalName(i); String type = aMap.getType(i); String value = aMap.getValue(i); System.out.println(" attribute name = " + attName + " type = " + type + " value = " + value); } Qualified Name

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

OCT27 C:\McCarthy\www\95-733\examples\sax>java NotifyStr FixedFloatSwap.xml startDocument called: startElement called: element name =FixedFloatSwap startElement called: element name =Bank characters called:Pittsburgh National Corporation startElement called: element name =Notional attribute name = currency type = dollars|pounds value = pounds characters called:100 startElement called: element name =Fixed_Rate characters called:5 startElement called: element name =NumYears characters called:3 startElement called: element name =NumPayments characters called:6 endDocument called: Output

OCT28 Accessing the swap from the internet <!DOCTYPE FixedFloatSwap [ ] > &bankname; Saved under webapps/sax/fpml/FixedFloatSwap.xml

OCT29 The Deployment Descriptor <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" " SaxExample GetXML SaxExample /GetXML/* webapps/sax/WEB-INF/web.xml

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

OCT31 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { System.out.println("doGet called with " + req.getPathInfo()); String theData = ""; String extraPath = req.getPathInfo(); extraPath = extraPath.substring(1); // read the file try { // open file and create a DataInputStream FileInputStream theFile = new FileInputStream( "D:\\jakarta-tomcat-4.0.1\\webapps\\sax\\fpml\\“ +extraPath);

OCT32 InputStreamReader is = new InputStreamReader(theFile); BufferedReader br = new BufferedReader(is); // read the file into the string theData String thisLine; while((thisLine = br.readLine()) != null) { theData += thisLine + "\n"; } catch(Exception e) { System.err.println("Error " + e); }

OCT33 PrintWriter out = res.getWriter(); out.write(theData); System.out.println("Wrote document to client"); //System.out.println(theData); out.close(); }

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

OCT35 XMLReader reader = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser"); String serverString = " String fileName = argv[0]; InputSource inputSource = new InputSource(serverString + fileName); reader.setContentHandler(new TomcatNotifyStr()); reader.parse(inputSource); System.exit (0); }

OCT36 public TomcatNotifyStr() {} public void startDocument() throws SAXException { System.out.println("startDocument called:"); } public void endDocument() throws SAXException { System.out.println("endDocument called:"); }

OCT37 public void startElement(String namespaceURI, String localName, String qName, Attributes aMap) throws SAXException { System.out.println("startElement called: element name =" + localName); // examine the attributes for(int i = 0; i < aMap.getLength(); i++) { String attName = aMap.getLocalName(i); String type = aMap.getType(i); String value = aMap.getValue(i); System.out.println(" attribute name = " + attName + " type = " + type + " value = " + value); }

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

OCT39 Being served by the servlet <!DOCTYPE FixedFloatSwap [ ] > &bankname;

OCT40 C:\McCarthy\www\95-733\examples\sax>java TomcatNotifyStr FixedFloatSwap.xml startDocument called: startElement called: element name =FixedFloatSwap characters called: startElement called: element name =Bank characters called:Pittsburgh National Corporation characters called: startElement called: element name =Notional attribute name = currency type = CDATA value = pounds characters called:100 characters called: startElement called: element name =Fixed_Rate characters called:5 characters called: startElement called: element name =NumYears characters called:3 characters called: startElement called: element name =NumPayments characters called:6 characters called: endDocument called: Output

OCT41 Let’s Add Back the DTD… <!ELEMENT FixedFloatSwap ( Bank, Notional, Fixed_Rate, NumYears, NumPayments ) >

OCT42 And reference the DTD in the XML <!DOCTYPE FixedFloatSwap SYSTEM "FixedFloatSwap.dtd" [ ] > &bankname;

OCT43 We get new output How many times did we visit the servlet? Twice. Once for the xml and a second time for the DTD. C:\McCarthy\www\95-733\examples\sax>java TomcatNotifyStr FixedFloatSwap.xml startDocument called: startElement called: element name =FixedFloatSwap startElement called: element name =Bank characters called:Pittsburgh National Corporation startElement called: element name =Notional attribute name = currency type = dollars|pounds value = pounds characters called:100 startElement called: element name =Fixed_Rate characters called:5 startElement called: element name =NumYears characters called:3 startElement called: element name =NumPayments characters called:6 endDocument called:

OCT44 We don’t have to go through a servlet…Tomcat can send the files String serverString = " String fileName = argv[0]; InputSource is = new InputSource(serverString + fileName); But the servlet illustrates that the XML data can be generated dynamically.

OCT45 The InputSource 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 InputSource object can be used to provide input to the parser. InputSurce SAX or DOM Tree Events application So, how do we build an InputSource object?

OCT46 Some InputSource constructors: InputSource(String pathToFileOrAURL); InputSource(InputStream byteStream); // bytes InputStream(Reader characterStream); // Characters For example: String text = “ some xml ”; StringReader sr = new StringReader(text); InputSource is = new InputSource(sr); : myParser.parse(is); Pass The InputSource Class to the Parser

OCT47 But what about the DTD? public interface EntityResolver 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 setEntityResolver 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.

OCT48 EntityResolver public InputSource resolveEntity(String publicId, String systemId) { // Add this method to the client above. The systemId String // holds the path to the dtd as specified in the xml document. // We could now build an InputStream object from a different // systemID and return this new InputStream. Or simply return null // and let the parser resolve the external entity. System.out.println("Attempting to resolve" + "Public id :" + publicId + "System id :" + systemId); return null; }

OCT49 The following examples were tested using Sun’s JAXP (JDK1.4) Processing XML with DOM

OCT50 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.

OCT51 I am The Cat in The Hat I am Little Cat A I am Little Cat B I am Little Cat C The XML File “cats.xml”

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

OCT53 Agreement.xml

OCT54 document XML doc doctype FixedFloatSwap Notional FixedRate NumYearsNumPayments All of these nodes implement the Node interface

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

OCT56 Some DOM Documentation from JavaSoft

OCT57 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.

OCT58 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…

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

OCT60 Some Methods of Node Example Methods are: short getNodeType() Might return a constant like ELEMENT_NODE or TEXT_NODE or …

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

OCT62 Some Methods of Node Example Methods are: Node getParentNode() returns a reference to the parent

OCT63 Some Methods of Node Example Methods are: public Node getFirstChild() Returns the value of the firstChild property.

OCT64 Some Methods of Node Example Methods are: public NodeList getChildNodes() returns a NodeList object NodeList is an interface and not a Node.

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

OCT66 There are only two methods of the NodeList 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.

OCT67 There are only two methods of the NodeList Interface public int getLength() Returns the value of the length property.

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

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

OCT70 The Element Interface public interface Element extends Node Some methods in the Element interface public String getTagName() Returns the value of the tagName property.

OCT71 The Element Interface public interface Element extends Node Some methods in the Element interface public NodeList getElementsByTagName(String name) Returns a NodeList 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..

OCT72 The CharacterData Interface public interface CharacterData extends Node The CharacterData 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 CharacterData, though Text and others do inherit the interface from it. All offsets in this interface start from 0.

OCT73 The CharacterData Interface public interface CharacterData extends Node An example method: public String getData() Returns the value of the the character data of the node that implements this interface. The Text interface extends CharacterData. public void setData(String data) is also available.

OCT74 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.

OCT75 The Document Interface public interface Document extends Node Some methods: public Element getDocumentElement() Returns the value of the documentElement 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 tagName "HTML".

OCT76 The Document Interface Some methods: public NodeList getElementsByTagName(String tagname) Returns a NodeList 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 NodeList object containing all the matched Elements.

OCT77 FixedFloatSwap.xml

OCT78 document XML doc doctype FixedFloatSwap Notional FixedRate NumYearsNumPayments FixedFloatSwap.xml

OCT79 An Example import java.io.File; import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; Process a local file

OCT80 public class Simulator3 { public static void main(String argv[]) { Document doc; if(argv.length != 1 ) { System.err.println("usage: java Simulator3 documentname"); System.exit(1); } try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

OCT81 doc = docBuilder.parse(new File(argv[0])); Element top = doc.getDocumentElement(); NodeList elementList = top.getElementsByTagName("*"); int listLength = elementList.getLength(); for(int i = 0; i < listLength; i++) { Element e = (Element)elementList.item(i); System.out.print(e.getNodeName()); Text t = (Text)e.getFirstChild(); System.out.println(t.getNodeValue()); }

OCT82 } catch(SAXParseException err) { System.out.println("Parsing error" + ", line " + err.getLineNumber() + ", URI " + err.getSystemId()); System.out.println(" " + err.getMessage()); } catch(SAXException e) { Exception x = e.getException(); ((x == null) ? e : x).printStackTrace(); } catch (Throwable t) { t.printStackTrace(); } System.exit(0); }

OCT83 FixedFloatSwap.xml

OCT84 Output Notional100 Fixed_Rate5 NumYears3 NumPayments6

OCT85 Read-Process-Write XML 100 MyGradeBook.xml

OCT86 ChangeGrade.java import java.io.File; import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import java.io.ByteArrayOutputStream;

OCT87 public class ChangeGrade { public static void main(String argv[]) { Document doc; if(argv.length != 2 ) { System.err.println( "usage: java ChangeGrade GradebookName newScore"); System.exit(1); } try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

OCT88 // read and parse the document and build the Dom tree doc = docBuilder.parse(new File(argv[0])); // get the score String newScore = (new Integer(argv[1])).toString(); // get document element Element top = doc.getDocumentElement(); // get and change score element NodeList anotherList = top.getElementsByTagName("Score"); Element score = (Element)anotherList.item(0); Text t = (Text)score.getFirstChild(); t.setNodeValue(newScore);

OCT89 // write the document to a file OutputFormat formatter = new OutputFormat(); XMLSerializer serializer = new XMLSerializer(System.out, formatter); serializer.serialize(doc); // write the document to a byte array ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer = new XMLSerializer(baos, formatter); serializer.serialize(doc); // fill an array of bytes byte[] bytes = baos.toByteArray();

OCT90 // convert to string String s = baos.toString(); System.out.println("As a String"); System.out.println(s); } catch(SAXParseException err) { System.out.println("Parsing error" + ", line " + err.getLineNumber() + ", URI " + err.getSystemId()); System.out.println(" " + err.getMessage()); } catch(SAXException e) { Exception x = e.getException(); ((x == null) ? e : x).printStackTrace(); } catch (Throwable t) { t.printStackTrace(); } System.exit(0); }

OCT91 D:\McCarthy\www\95-733\examples\dom> java ChangeGrade MyGradeBook.xml As a String 34

OCT92 Using the InputSource class D:\McCarthy\www\95-733\examples\dom\internetexample>tree /f Directory PATH listing Volume serial number is 0012FC94 486D:D392 D:. │ build.properties │ build.xml ├───build │ │ MyGradeBook.xml │ │ └───WEB-INF │ ├───classes │ └───lib ├───src └───web MyGradeBook.xml

OCT93 Using the InputSource Class import java.io.File; import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import java.io.ByteArrayOutputStream; import org.xml.sax.InputSource;

OCT94 public class ReadGradesFromInternet { public static void main(String argv[]) { Document doc; try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); InputSource inputSource = new InputSource( " // read and parse the document and build the Dom tree doc = docBuilder.parse(inputSource);

OCT95 // write the document to a file OutputFormat formatter = new OutputFormat(); XMLSerializer serializer = new XMLSerializer( System.out, formatter); serializer.serialize(doc); } catch(SAXParseException err) { System.out.println("Parsing error" + ", line " + err.getLineNumber() + ", URI " + err.getSystemId()); System.out.println(" " + err.getMessage()); } catch(SAXException e) { Exception x = e.getException(); ((x == null) ? e : x).printStackTrace(); } catch (Throwable t) { t.printStackTrace(); } System.exit(0); }}

OCT96 D:\McCarthy\www\95-733\examples\dom> java ReadGradesFromInternet 100

OCT97 Example - A TreePrint Class import org.w3c.dom.*; public class TreePrinter { private Document doc; private int currentIndent; public TreePrinter(Document d) { currentIndent = 2; doc = d; } public void print() { privatePrint(doc,currentIndent); }

OCT98 document XML doc doctype FixedFloatSwap Notional FixedRate NumYearsNumPayments FixedFloatSwap.xml

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

OCT100 case n.COMMENT_NODE: System.out.println(" " + "...Comment node"); break; case n.ENTITY_NODE: System.out.println("ENTITY "+ n.getNodeName()+ "...Entity Node"); break; case n.ENTITY_REFERENCE_NODE: System.out.println("&"+n.getNodeName()+";" + "...Entity Reference Node"); break; case n.DOCUMENT_TYPE_NODE: System.out.println("DOCTYPE"+n.getNodeName()+ "...Document Type Node"); break; default: System.out.println("?" + n.getNodeName()); } Node child = n.getFirstChild(); while(child != null) { privatePrint(child, indent+currentIndent); child = child.getNextSibling(); }

OCT101 Output C:\McCarthy\www\Financial Engineering\FixedFloatSwap>java Simulator6 No Problems found #document...Document Node DOCTYPEFixedFloatSwap...Document Type Node FixedFloatSwap...Element Node #text...Text Node Notional...Element Node #text...Text Node Fixed_Rate...Element Node #text...Text Node NumYears...Element Node #text...Text Node NumPayments...Element Node #text...Text Node

OCT102 Building a DOM Tree From Scratch 100 Let’s create this file from within a java program. MyGradeBook.xml

OCT103 GOAL C:\McCarthy\www\95-733\examples\dom>java DomExample C:\McCarthy\www\95-733\examples\dom>type MyGradeBook.xml 100

OCT104 // DomExample.java // Building an xml document from scratch import java.io.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.*; import org.apache.xml.serialize.XMLSerializer; // not standard import org.apache.xml.serialize.OutputFormat; // not standard

OCT105 public class DomExample { private Document document; public DomExample () { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.newDocument(); } catch (Throwable t) { t.printStackTrace (); }

OCT106 // Ask the Document object for various types // of nodes and // add them to the tree. Element root = document.createElement("GradeBook"); document.appendChild(root); Element student = document.createElement("Student"); root.appendChild(student); Element score = document.createElement("Score"); student.appendChild(score); Text value = document.createTextNode("100"); score.appendChild(value);

OCT107 // Write the Document to disk using Xerces. try { FileOutputStream fos = new FileOutputStream( "MyGradeBook.xml"); XMLSerializer xmlWriter = new XMLSerializer(fos, null); xmlWriter.serialize(document); } catch(IOException ioe) { ioe.printStackTrace(); }

OCT108 public static void main(String a[]) { DomExample tree = new DomExample(); }

OCT109 A SAX Example Using a Factory // SimpleSAX.java import java.io.*; import org.xml.sax.*; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import org.xml.sax.helpers.DefaultHandler;

OCT110 public class SimpleSAX extends DefaultHandler { public static void main (String argv []) { if (argv.length != 1) { System.err.println ( // enter a local or distant file name "Usage: java SimpleSAX XMLFileURL"); System.exit (1); }

OCT111 String fileNameURL = argv[0]; System.out.println("Will attempt to process " + fileNameURL); InputSource is = new InputSource(fileNameURL); SimpleSAX myHandler = new SimpleSAX(is); System.out.println("Processed " + myHandler.getCounter()); System.exit (0); }

OCT112 SAXParserFactory factory = SAXParserFactory.newInstance(); int counter = 0; public SimpleSAX(InputSource is) { factory.setValidating(false); try { SAXParser saxParser = factory.newSAXParser(); saxParser.parse( is, this); } catch (Throwable t) { System.out.println("Error in constructor"); t.printStackTrace (); }

OCT113 public int getCounter() { return counter; } public void startDocument() throws SAXException { System.out.println("startDocument called:"); } public void endDocument() throws SAXException { System.out.println("endDocument called:"); }

OCT114 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { counter++; System.out.println("startElement called: element name =" + qName); } public void endElement(String name) throws SAXException { System.out.println("endElement is called:" + name); } public void characters(char[] ch, int start, int length) throws SAXException { // build String from char array String dataFound = new String(ch,start,length); System.out.println("characters called:" + dataFound); }

OCT115 public void error(SAXParseException e) throws SAXException { System.out.println("Parsing error"); System.out.println(e.toString()); }