5.XML Processing
5.XML Processing
R. LOGAMBIGAI, TA
December 8, 2016
DOM
DOM (Document Object Model)
It is an object model for representing XML documents
in your code.
Using DOM we can create or modify an XML document
programmatically.
The DOM defines theobjects and propertiesof all
document elements, and themethods(interface) to
access them.
.3
R. LOGAMBIGAI, TA
December 8, 2016
<?xml?>
XML Document
Loads
document
Parses declarations
Builds DTD
Application
4
R. LOGAMBIGAI, TA
December 8, 2016
DOM Levels
Core DOM - standard model for any structured document
XML DOM - standard model for XML documents
HTML DOM - standard model for HTML documents
R. LOGAMBIGAI, TA
December 8, 2016
XML DOM
The XML DOM is:
A standard object model for XML
A standard programming interface for XML
Platform- and language-independent
A W3C standard
The XML DOM defines theobjects and propertiesof all
XML elements, and themethods(interface) to access
them.
R. LOGAMBIGAI, TA
December 8, 2016
DOM Nodes
R. LOGAMBIGAI, TA
December 8, 2016
Generic Form
XML
Document
R. LOGAMBIGAI, TA
Parser
DOM
Application
Programme
December 8, 2016
R. LOGAMBIGAI, TA
December 8, 2016
XML Document
Information to be represented in the DOM
structure.
<?xml version="1.0" encoding="UTF-8"?>
<entry id="Baker2005">
<author>Mark Baker and Amy W. Apon and Clayton Ferner and Jeff
Brown</author>
<title>Emerging Grid Standards</title>
<journal>IEEE Computer</journal>
<year>2005</year>
<volume>38</volume>
<pages>43-50</pages>
<number>4</number>
</entry>
10
R. LOGAMBIGAI, TA
December 8, 2016
Information To Be Represented
Document
Attributes
id
<citation.xml>
Root Element
<entry>
<etc>
<author>
<title>
<journal>
11
R. LOGAMBIGAI, TA
<volume>
<year>
December 8, 2016
DOM Example
<?xml version=1.0?>
Node
<addressbook>
<person>
addressbook
Node
<name>John Doe</name>
<email>[email protected]</email>
</person>
<person>
<name>Jane Doe</name>
<email>[email protected]</email>
XML
Parser
Node
person
Node
Name=John Doe
Node
person
Node
Name=John Doe
Node
</person>
</addressbook>
12
R. LOGAMBIGAI, TA
December 8, 2016
DOM Representation
Document
Node
Document Root
<Parent>
NodeList
<Parent>
</Parent>
NodeList
Element
Node
<Child>
NamedNodeMap
Attribute
Node
<id=123>
NodeList
Text CDATA
Node
13
R. LOGAMBIGAI, TA
Text here
December 8, 2016
R. LOGAMBIGAI, TA
December 8, 2016
18
R. LOGAMBIGAI, TA
December 8, 2016
SAX features
SAX API acts like a data stream.
Stateless.
Events are not permanent.
Data not stored in memory.
Impossible to move backward in XML data.
Impossible to modify document structure.
Fastest and least memory intensive way of working
with XML.
19
R. LOGAMBIGAI, TA
December 8, 2016
R. LOGAMBIGAI, TA
December 8, 2016
R. LOGAMBIGAI, TA
December 8, 2016
<?xml version="1.0"?>
startDocument()
<xmlExample>
startElement(): xmlExample
<heading>
This is a simple
example.
characters():
endElement(): heading
</heading>
endElement(): xmlExample
</xmlExample>
22
startElement(): heading
R. LOGAMBIGAI, TA
endDocument()
December 8, 2016
DefaultHandler class
Class
org.xml.sax.helpers.DefaultHandler:
Implements all four handle interfaces with
null methods,
Programmer can derive from
DefaultHandler his own class and pass its
instance to a parser,
Programmer can override only methods
responsible for some events and ignore the
rest.
24
R. LOGAMBIGAI, TA
December 8, 2016
SAX Objects
<?xml version=1.0?>
Parser
startDocument
<addressbook>
Parser
startElement
<name>John Doe</name>
Parser
<email>[email protected]</email>
Parser
</person>
Parser
endElement
<person>
Parser
startElement
<name>Jane Doe</name>
Parser
<email>[email protected]</email>
Parser
Parser
endElement
Parser
<person>
</person>
</addressbook>
25
R. LOGAMBIGAI, TA
December 8, 2016
(1) It is simple
(2) It is memory efficient
(3) It works well in stream application
Disadvantage:
The data is broken into pieces and
clients never have all the information as
a whole unless they create their own
data structure
26
R. LOGAMBIGAI, TA
December 8, 2016
SAX
27
R. LOGAMBIGAI, TA
December 8, 2016
R. LOGAMBIGAI, TA
December 8, 2016
29
R. LOGAMBIGAI, TA
December 8, 2016
30
R. LOGAMBIGAI, TA
December 8, 2016