Java and XML (DOM and SAX) - PowerPoint PPT Presentation

About This Presentation
Title:

Java and XML (DOM and SAX)

Description:

Some methods are: void error(SAXParseException exception) ... Being served by the servlet ?xml version='1.0' encoding='UTF-8'? !DOCTYPE FixedFloatSwap ... – PowerPoint PPT presentation

Number of Views:717
Avg rating:3.0/5.0
Slides: 109
Provided by: mm77
Category:
Tags: dom | sax | xml | are | being | java | served | you

less

Transcript and Presenter's Notes

Title: Java and XML (DOM and SAX)


1
Java and XML (DOM and SAX)
Some of the material for these slides came from
the following sources XML a
Managers 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
2
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)

3
FixedFloatSwap.xml
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
FixedFloatSwap SYSTEM "FixedFloatSwap.dtd"gt ltFixed
FloatSwapgt ltNotionalgt100lt/Notionalgt
ltFixed_Rategt5lt/Fixed_Rategt ltNumYearsgt3lt/NumYea
rsgt ltNumPaymentsgt6lt/NumPaymentsgt lt/FixedFloatS
wapgt
4
FixedFloatSwap.dtd
lt?xml version"1.0" encoding"utf-8"?gt lt!ELEMENT
FixedFloatSwap (Notional, Fixed_Rate, NumYears,

NumPayments ) gt lt!ELEMENT Notional (PCDATA)
gt lt!ELEMENT Fixed_Rate (PCDATA) gt lt!ELEMENT
NumYears (PCDATA) gt lt!ELEMENT NumPayments
(PCDATA) gt
5
Operation of a Tree-based Parser
XML DTD
Document Tree
Tree-Based Parser
Application Logic
Valid
XML Document
6
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)

7
Operation of an Event Based Parser
XML DTD
Event-Based Parser
Application Logic
Valid
XML Document
8
Operation of an Event Based Parser
XML DTD
public void startDocument () public void
endDocument () public void startElement
()) public void endElement () public void
characters ())
Event-Based Parser
Application Logic
Valid
public void error(SAXParseException e) throws
SAXException System.out.println("\n
\n--Invalid document ---" e)
XML Document
9
Event-Driven Benefits
  • We do not need the memory required for trees
  • Parsing can be done faster with no tree
    construction going on

10
Some of the XML APIs in JDK1.4
11
There are more in JWSDP
12
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
13
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.

14
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

15
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.
16
Some methods from the ContentHandler Interface
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.
17
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.
18
public interface ErrorHandler Some methods
are void error(SAXParseException exception)
Receive notification of a
recoverable error. void fatalError(SAXParseExcepti
on exception) Receive
notification of a non-recoverable error. void
warning(SAXParseException exception)
Receive notification of a warning.
19
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.
20
FixedFloatSwap.dtd
lt?xml version"1.0" encoding"utf-8"?gt lt!ELEMENT
FixedFloatSwap ( Bank, Notional, Fixed_Rate,
NumYears,
NumPayments ) gt lt!ELEMENT Bank
(PCDATA)gt lt!ELEMENT Notional (PCDATA)gt lt!ATTLIST
Notional currency (dollars pounds)
REQUIREDgt lt!ELEMENT Fixed_Rate (PCDATA)
gt lt!ELEMENT NumYears (PCDATA) gt lt!ELEMENT
NumPayments (PCDATA) gt
Input DTD
21
FixedFloatSwap.xml
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
FixedFloatSwap SYSTEM "FixedFloatSwap.dtd"
lt!ENTITY bankname "Pittsburgh National
Corporation"gt gt ltFixedFloatSwapgt
ltBankgtbanknamelt/Bankgt ltNotional currency
"pounds"gt100lt/Notionalgt
ltFixed_Rategt5lt/Fixed_Rategt
ltNumYearsgt3lt/NumYearsgt ltNumPaymentsgt6lt/NumP
aymentsgt lt/FixedFloatSwapgt
Input XML
22
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
23
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(argv0)
reader.setContentHandler(new NotifyStr())
reader.parse(inputSource)
System.exit (0)
24
public NotifyStr() public void
startDocument() throws SAXException
System.out.println("startDocument called")
public void endDocument() throws
SAXException System.out.println("endDo
cument called")
25
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 lt
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)

ltpreLocalPartgt
Qualified Name
26
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)
27
C\McCarthy\www\95-733\examples\saxgtjava
NotifyStr FixedFloatSwap.xml startDocument
called startElement called element name
FixedFloatSwap startElement called element name
Bank characters calledPittsburgh National
Corporation startElement called element name
Notional attribute name currency type
dollarspounds value pounds characters
called100 startElement called element name
Fixed_Rate characters called5 startElement
called element name NumYears characters
called3 startElement called element name
NumPayments characters called6 endDocument
called
Output
28
Accessing the swap from the internet
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
FixedFloatSwap lt!ENTITY bankname "Pittsburgh
National Corporation"gt gt ltFixedFloatSwapgt
ltBankgtbanknamelt/Bankgt ltNotional
currency "pounds"gt100lt/Notionalgt
ltFixed_Rategt5lt/Fixed_Rategt
ltNumYearsgt3lt/NumYearsgt ltNumPaymentsgt6lt/NumP
aymentsgt lt/FixedFloatSwapgt
Saved under webapps/sax/fpml/FixedFloatSwap.xml
29
The Deployment Descriptor
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
web-app PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.2//EN"
"http//java.sun.com/j2ee/dtds/web-app_2.2.dtd"gt
ltweb-appgt ltservletgt ltservlet-namegtSaxExamp
lelt/servlet-namegt ltservlet-classgtGetXMLlt/servl
et-classgt lt/servletgt ltservlet-mappinggt
ltservlet-namegtSaxExamplelt/servlet-namegt
lturl-patterngt/GetXML/lt/url-patterngt
lt/servlet-mappinggt lt/web-appgt
webapps/sax/WEB-INF/web.xml
30
// 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. pub
lic class GetXML extends HttpServlet
// Servlet
31
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)

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

33
PrintWriter out
res.getWriter()
out.write(theData)
System.out.println("Wrote document to client")
//System.out.println
(theData)
out.close()
34
// 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
35
XMLReader reader XMLReaderFactory.creat
eXMLReader(
"org.apache.xerces.parsers.SAXParser")
String serverString "http//localhost8080/sax/G
etXML/" String fileName argv0
InputSource inputSource new
InputSource(serverString

fileName)
reader.setContentHandler(new TomcatNotifyStr())
reader.parse(inputSource)
System.exit (0)
36
public TomcatNotifyStr() public void
startDocument() throws SAXException
System.out.println("startDocument called")
public void endDocument() throws
SAXException System.out.println("endDo
cument called")
37
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 lt
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)

38
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)
39
Being served by the servlet
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
FixedFloatSwap lt!ENTITY bankname "Pittsburgh
National Corporation"gt gt ltFixedFloatSwapgt
ltBankgtbanknamelt/Bankgt ltNotional
currency "pounds"gt100lt/Notionalgt
ltFixed_Rategt5lt/Fixed_Rategt
ltNumYearsgt3lt/NumYearsgt ltNumPaymentsgt6lt/NumP
aymentsgt lt/FixedFloatSwapgt
40
C\McCarthy\www\95-733\examples\saxgtjava
TomcatNotifyStr FixedFloatSwap.xml startDocument
called startElement called element name
FixedFloatSwap characters called startElement
called element name Bank characters
calledPittsburgh National Corporation characters
called startElement called element name
Notional attribute name currency type
CDATA value pounds characters
called100 characters called startElement
called element name Fixed_Rate characters
called5 characters called startElement called
element name NumYears characters
called3 characters called startElement called
element name NumPayments characters
called6 characters called characters
called endDocument called
Output
41
Lets Add Back the DTD
lt?xml version"1.0" encoding"utf-8"?gt lt!ELEMENT
FixedFloatSwap ( Bank, Notional, Fixed_Rate,
NumYears,
NumPayments ) gt lt!ELEMENT Bank
(PCDATA)gt lt!ELEMENT Notional (PCDATA)gt lt!ATTLIST
Notional currency (dollars pounds)
REQUIREDgt lt!ELEMENT Fixed_Rate (PCDATA)
gt lt!ELEMENT NumYears (PCDATA) gt lt!ELEMENT
NumPayments (PCDATA) gt
42
And reference the DTD in the XML
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
FixedFloatSwap SYSTEM "FixedFloatSwap.dtd"
lt!ENTITY bankname "Pittsburgh National
Corporation"gt gt ltFixedFloatSwapgt
ltBankgtbanknamelt/Bankgt ltNotional currency
"pounds"gt100lt/Notionalgt
ltFixed_Rategt5lt/Fixed_Rategt
ltNumYearsgt3lt/NumYearsgt ltNumPaymentsgt6lt/NumP
aymentsgt lt/FixedFloatSwapgt
43
We get new output
C\McCarthy\www\95-733\examples\saxgtjava
TomcatNotifyStr FixedFloatSwap.xml startDocument
called startElement called element name
FixedFloatSwap startElement called element name
Bank characters calledPittsburgh National
Corporation startElement called element name
Notional attribute name currency type
dollarspounds value pounds characters
called100 startElement called element name
Fixed_Rate characters called5 startElement
called element name NumYears characters
called3 startElement called element name
NumPayments characters called6 endDocument
called
How many times did we visit the servlet? Twice.
Once for the xml and a second time for the DTD.
44
We dont have to go through a servletTomcat can
send the files
String serverString "http//localhost80
80/sax/fpml/" String fileName
argv0 InputSource is new
InputSource(serverString fileName)
But the servlet illustrates that the XML data can
be generated dynamically.
45
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.
Tree
application
InputSurce
SAX or DOM
Events
So, how do we build an InputSource object?
46
Pass The InputSource Class to the Parser
Some InputSource constructors
InputSource(String pathToFileOrAURL)
InputSource(InputStream byteStream) //
bytes InputStream(Reader characterStream)
// Characters For example String text
ltagtsome xmllt/agt StringReader sr new
StringReader(text) InputSource is new
InputSource(sr) myParser.parse(is)
47
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.
48
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
49
Processing XML with DOM
  • The following examples were tested using Suns
    JAXP
  • (JDK1.4)

50
XML DOM
  • The World Wide Web Consortiums 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.

51
The XML File cats.xml
lt?xml version "1.0" ?gt lt!DOCTYPE TopCat SYSTEM
"cats.dtd"gt ltTopCatgt I am The Cat in The
Hat ltLittleCatAgt I am Little
Cat A lt/LittleCatAgt ltLittleCatBgt
I am Little Cat B
ltLittleCatCgt I am Little Cat C
lt/LittleCatCgt lt/LittleCatBgt
ltLittleCatD/gt lt/TopCatgt
52
document
XML doc
Called the Document Element
doctype
element
topcat
element
text
element
element
Little cat B
Little cat A
I am the cat in the hat
Little cat D
element
text
text
Little Cat C
I am little cat B
I am little cat A
text
I am little cat C
DOM
53
Agreement.xml
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
FixedFloatSwap SYSTEM "FixedFloatSwap.dtd"gt ltFixed
FloatSwapgt ltNotionalgt100lt/Notionalgt
ltFixed_Rategt5lt/Fixed_Rategt ltNumYearsgt3lt/NumYea
rsgt ltNumPaymentsgt6lt/NumPaymentsgt lt/FixedFloatS
wapgt
54
document
XML doc
doctype
FixedFloatSwap
Notional
NumYears
NumPayments
FixedRate
100
5
3
6
All of these nodes implement the Node interface
55
Operation of a Tree-based Parser
XML DTD
Document Tree
Tree-Based Parser
Application Logic
Valid
XML Document
56
Some DOM Documentation from JavaSoft
57
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.

58
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

59
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
60
Some Methods of Node
Example Methods are short getNodeType()
Might return a constant like
ELEMENT_NODE or TEXT_NODE or

61
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.
62
Some Methods of Node
Example Methods are Node getParentNode()
returns a reference to the
parent
63
Some Methods of Node
Example Methods are public Node
getFirstChild() Returns the
value of the firstChild property.
64
Some Methods of Node
Example Methods are public NodeList
getChildNodes() returns a
NodeList object
NodeList is an interface and not a Node.
65
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.

66
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.
67
There are only two methods of the NodeList
Interface
public int getLength() Returns the value of
the length property.
68
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.

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

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

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

72
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.
73
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.
74
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.
75
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".
76
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.
77
FixedFloatSwap.xml
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
FixedFloatSwap SYSTEM "FixedFloatSwap.dtd"gt ltFixed
FloatSwapgt ltNotionalgt100lt/Notionalgt
ltFixed_Rategt5lt/Fixed_Rategt ltNumYearsgt3lt/NumYea
rsgt ltNumPaymentsgt6lt/NumPaymentsgt lt/FixedFloatS
wapgt
78
document
XML doc
doctype
FixedFloatSwap
Notional
NumYears
NumPayments
FixedRate
100
5
3
6
FixedFloatSwap.xml
79
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
80
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()

81
doc docBuilder.parse(new
File(argv0))
Element top doc.getDocumentElement()
NodeList elementList
top.getElementsByTagName("") int
listLength elementList.getLength()
for(int i 0 i lt listLength i)
Element e
(Element)elementList.item(i)
System.out.print(e.getNodeName())
Text t (Text)e.getFirstChild()
System.out.println(t.getNodeValue())

82
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)
83
FixedFloatSwap.xml
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
FixedFloatSwap SYSTEM "FixedFloatSwap.dtd"gt ltFixed
FloatSwapgt ltNotionalgt100lt/Notionalgt
ltFixed_Rategt5lt/Fixed_Rategt ltNumYearsgt3lt/NumYea
rsgt ltNumPaymentsgt6lt/NumPaymentsgt lt/FixedFloatS
wapgt
84
Output
Notional100 Fixed_Rate5 NumYears3 NumPayments6
85
Read-Process-Write XML
MyGradeBook.xml
lt?xml version"1.0"?gt ltGradeBookgt
ltStudentgt ltScoregt100lt/Scoregt
lt/Studentgt lt/GradeBookgt
86
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
87
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()

88
// read and parse the document
and build the Dom tree doc
docBuilder.parse(new File(argv0))
// get the score String newScore
(new Integer(argv1)).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)
89
// write the document to a file
OutputFormat formatter new OutputFormat()
XMLSerializer serializer new XMLSerializer(Syste
m.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()
90
// 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)
91
D\McCarthy\www\95-733\examples\domgt java
ChangeGrade MyGradeBook.xml 34 lt?xml
version"1.0" encoding"UTF-8"?gt ltGradeBookgt
ltStudentgt ltScoregt34lt/Scoregt
lt/Studentgt lt/GradeBookgt As a String lt?xml
version"1.0" encoding"UTF-8"?gt ltGradeBookgt
ltStudentgt
ltScoregt34lt/Scoregt lt/Studentgt
lt/GradeBookgt
92
Using the InputSource class
D\McCarthy\www\95-733\examples\dom\internetexampl
egttree /f Directory PATH listing Volume serial
number is 0012FC94 486DD392 D.
build.properties build.xml ---build
MyGradeBook.xml ---WEB-INF
---classes ---lib ---src ---web
MyGradeBook.xml
93
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
94
public class ReadGradesFromInternet public
static void main(String argv)
Document doc try
DocumentBuilderFactory docBuilderFactory

DocumentBuilderFactory.newInstance()
DocumentBuilder docBuilder
docBuilderFactory.newDocumen
tBuilder()
InputSource inputSource new InputSource(
"http//localhost8080/Grades/MyG
radeBook.xml") // read and parse the
document and build the Dom tree
doc docBuilder.parse(inputSource)

95
// 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)

96
D\McCarthy\www\95-733\examples\domgt java
ReadGradesFromInternet lt?xml version"1.0"
encoding"UTF-8"?gt ltGradeBookgt ltStudentgt
ltScoregt100lt/Scoregt lt/Studentgt
lt/GradeBookgt
97
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)
98
document
XML doc
doctype
FixedFloatSwap
Notional
NumYears
NumPayments
FixedRate
100
5
3
6
FixedFloatSwap.xml
99
public void privatePrint(Node n, int indent)
for(int i 0 i lt 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("lt?"n.getNodeName()"...?gt"
"...PI Node") break

100
case n.COMMENT_NODE
System.out.println("lt!--"n.getNodeValue()"--gt"
"...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.getNod
eName() "...Document Type Node")
break default
System.out.println("?" n.getNodeName())
Node child n.getFirstChild()
while(child ! null)
privatePrint(child, indentcurrentIndent)
child child.getNextSibling()

101
Output
C\McCarthy\www\Financial Engineering\FixedFloatSw
apgtjava Simulator6 No Problems found
document...Document Node DOCTYPEFixedFloatSwa
p...Document Type Node FixedFloatSwap...Elemen
t Node text...Text Node
Notional...Element Node text...Text
Node text...Text Node
Fixed_Rate...Element Node text...Text
Node text...Text Node
NumYears...Element Node text...Text
Node text...Text Node
NumPayments...Element Node text...Text
Node text...Text Node
102
Building a DOM Tree From Scratch
MyGradeBook.xml
lt?xml version"1.0" encoding"UTF-8"?gt ltGradeBook
gt ltStudentgt ltScoregt100lt/Scoregt
lt/Studentgt lt/GradeBookgt
Lets create this file from within a java program.
103
GOAL
C\McCarthy\www\95-733\examples\domgtjava
DomExample C\McCarthy\www\95-733\examples\domgtty
pe MyGradeBook.xml lt?xml version"1.0"?gt ltGradeBoo
kgtltStudentgtltScoregt100lt/Scoregtlt/Studentgtlt/GradeBook
gt
104
// 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 im
port org.w3c.dom. import org.apache.xml.serializ
e.XMLSerializer // not standard import
org.apache.xml.serialize.OutputFormat // not
standard
105
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 ()
106
// Ask the Document object for
various types // of nodes and
// add them to the tree.
Element root document.createElement("GradeBo
ok") document.appendChild(root)
Element student
document.createElement("Student")
root.appendChild(student)
Element score document.createElement("Sc
ore") student.appendChild(score)
Text value
document.createTextNode("100")
score.appendChild(value)
107
// 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()

108
public static void main(String a)
DomExample tree new DomExample()
Write a Comment
User Comments (0)
About PowerShow.com