XML Parsers (Dom Sax)
XML Parsers (Dom Sax)
DOM Parser
SAX Parser
DOM (Document Object Model)
• DOM is a platform that allows programs and scripts to
dynamically access and update the content and structure of a
XML documents.
• The DOM parser functions are part of the PHP core. There is
no installation needed to use these functions.
$xmlDoc->load("note.xml");
this statement loads a xml file by using object.
DOM (Document Object Model)
These are some typical DOM properties in php:
• X -> nodeName - the name of X
• X -> nodeValue - the value of X
• X->parentNode - the parent node of X
• X->childNodes - the child nodes of X
• X->attributes - the attributes nodes of X
Where X is Node object.
“note.xml”
<?xml version="1.0" encoding="UTF-8"?>
<student>
<num>521</num>
<name>xyz</name>
<age>30</age>
</student>
DOM (Document Object Model)
“Note.php”
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item) {
print $item->nodeValue . "<br>";
}
?>
Output:
SAX
Simple API for XML
XML Parsers
What is an XML parser?
– An XML parser is a software library or package
that provides interfaces for client applications to
work with an XML document.
– Large documents
– Memory constrained devices
– If you need not to modify the document
SAX Parser
Which languages are supported?
– Java
– Perl
– C++
– Python
SAX Implementation in Java
Import org.xml.sax.*;
import org.xml.sax.helpers.ParserFactory;
Public class SaxApplication extends HandlerBase {
public static void main(String args[]) {
}
}
SAX Implementation in Java
• Create a SAX Parser
DOM loads the file into the memory and SAX parses the file as it reads it, i.e.
then parse- the file. parses node by node.
Has memory constraints since it loads No memory constraints as it does not
the whole XML file before parsing. store the XML content in the memory.
DOM is read and write (can insert or SAX is read only i.e. can’t insert or
delete nodes). delete the node.
If the XML content is small, then prefer Use SAX parser when XML content is
DOM parser. large.
Backward and forward search is possible SAX reads the XML file from top to
for searching the tags and evaluation of bottom and backward navigation is not
the information inside the tags. possible.