XML Dom and Sax Parsers
XML Dom and Sax Parsers
XML Dom and Sax Parsers
In XML, a
parser is a
software
component
that sits
between the
application
and the XML
files.
Introduction to parsers
structural isomorphism.
DOM Identifies
document.
Defines an event model.
namespaces
DOM levels: DOM 3
Document loading and saving as well
as content models (such as DTD’s
and schemas) with document
validation support.
function convert(form,xmldocument)
{var fname = form.fname.value,
output = form.output,
rate = form.rate.value;
output.value = "";
var document = parse(fname,xmldocument),
topLevel = document.documentElement;
searchPrice(topLevel,output,rate);}
function parse(uri,xmldocument)
{xmldocument.async = false;
xmldocument.load(uri);
if(xmldocument.parseError.errorCode != 0)
alert(xmldocument.parseError.reason);
return xmldocument;}
function searchPrice(node,output,rate)
{if(node.nodeType == 1)
{if(node.nodeName == "price")
output.value += (getText(node) * rate) + "\r";
var children,
i;
children = node.childNodes;
for(i = 0;i < children.length;i++)
searchPrice(children.item(i),output,rate);}}
function getText(node)
{return node.firstChild.data;}
An Application of DOM
nodeType is a code representing the type of the object.
The function
searchPrice() visits
each node by
recursively calling
itself for all
children of the
current node.
An Application for DOM
What is SAX?
SAX (the Simple API for XML) is an event-
based parser for xml documents.
Content of elements
Entities
Parsing errors
SAX API
SAX Example
<?xml version="1.0"?>
<doc>
<para>Hello, world!</para>
</doc>
SAX example
start document
start element: doc
start element: para
characters: Hello, world!
end element: para
end element: doc
end document