1
XML Parsers
- Asst. Prof. Dr. Kanda Runapongsa Saikaew
(krunapon@kku.ac.th)
- Dept. of Computer Engineering
Khon Kaen University
XML Parsers Asst. Prof. Dr. Kanda Runapongsa Saikaew - - PowerPoint PPT Presentation
XML Parsers Asst. Prof. Dr. Kanda Runapongsa Saikaew (krunapon@kku.ac.th) Dept. of Computer Engineering Khon Kaen University 1 Overview What are XML Parsers? Programming Interfaces of XML Parsers DOM: Document Object Model
1
(krunapon@kku.ac.th)
Khon Kaen University
2
What are XML Parsers?
3
4
Parsers also check whether
There are two types of XML parsers
Validating: check documents
Non-validating: do not check
5
SUN
Integrated in JDK 1.4 version and later Package javax.xml.parsers
Apache Xerces: XML Parsers in Java,
http://xerces.apache.org/
SAX
http://www.saxproject.org/
XP – an XML Parser in Java
http://www.jclark.com/xml/xp/index.html
6
PHP and Java
Document Object Model (DOM)
Model a document as a tree
Java
Simple API for XML (SAX)
The user needs to create the model
Streaming API for XML (StAX)
Use a pull model for event processing Provide user-friendly APIs for read-in and
write-out
PHP
SimpleXML extension
Provides a very simple and easily usable toolset to
convert XML to an object
XMLReader extension
The reader acts as a cursor going forward on the
document stream and stopping at each node
XMLWriter extension
The writer that provides a non-cached, forward-
containing XML data
7
8
Create a parser object Point the parser object at your XML
Process the results
9
DOM is an official recommendation of
It defines an interface that enables
When an XML parser claims to
10
When you parse an XML
11
Each element node contains a
12
The DOM Processing Model
13
Inserting, editing, or deleting tree
14
SAX is an alternative way of working
It was designed to have a smaller
SAX does not crate a default object
SAX was originally developed by
15
When you parse an XML
16
17
StAX is an exciting new parsing
Like SAX, it uses an event-driven
However, instead of using SAX’s
Instead of using a callback mechanism,
18
SAX returns different types of event to the
StAX returns its events to the application
StAX includes factories for creating the
Applications can use the StAX interfaces
without reference to the details of a particular implementation
19
StAX specifies two parsing models
The cursor model The iterator model
Like SAX, the cursor model simply
The iterator model returns events as
Provide a more natural interface but has
the additional overhead of object creation
20
In the case of DOM, the parser does
Read the XML document in Create an object model on top of it Give you a reference to this object
SAX does not expect the parser to do
21
Read in the XML document Fire a bunch of events depending
22
it bypasses the creation of a tree
23
Partial parsing is not possible Loading the whole document and
The DOM tree is an order of magnitude
larger than the document
The generic DOM node type is an
24
When the development needs to be
DOM is quite easy to implement
When you need to have random
Example: An XSL Processor
When you need to modify an XML
Example: An XML Editor
25
You have to implement the event
Must maintain event states in your code Must keep track of where the parser is
in the document
It does not have built-in document
No random access support
26
When you have a small amount of
SAX requires little memory because it
When you need to only read the
Example: Many B2B and EAI applications use
XML just as an encapsulation format in which the receiving end simply retrieves all the data
27
It does not have built-in document
No random access support
Document modification is still quite
28
When applications need to take
For an application that can easily
Example: Web services
29
XML parsers are programs to read,
To automate the XML processing, XML
XML parsers APIs
DOM
+ Easy for developers to develop + Random access - Requires lots of memory
SAX, StAX
+ Fast processing - Developers need to create their own data model
ext/xmlreader and ext/xmlwriter Allow for XML to be read or written
Resulting in very low memory usage But providing very focused and
To manipulate XML data tree Using DOM or SimpleXML
30
DOM allows a developer to access
DOM is a large and complex API,
SimpleXML aims to break through all
31
The vast majority of people working
DOM models an XML document as a
SimpleXML takes an easier approach
Elements are represented as properties
and attributes as accessors
32
33
Sang Shin , “XML Course Page”
http://www.javapassion.com/xml/
Oracle, “Parsing XML Efficiently”
http://www.oracle.com/technology/oramag/oracle/0 3-sep/o53devxml.html
Zend Technologies, “XML and PHP5”,
http://devzone.zend.com/article/2387