Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit - 1 SAX

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

XML Parsing Using SAX

 Built-in Parsers: All major browsers have built-in parsers to access and
manipulate XML file.
 XML Parser: Convert text into XML object
1. DOM defines the properties and methods to access and edit XML elements
 Two types:
1. DOM – create an internal structure
2. SAX (Simple API for XML) – not creating an internal structure. Works like
for an event handler
 Working Principle of XML parser:

 SAX is alternative for DOM XML parser


 It is an event based parser for XML documents.
 SAX provides a way of reading data from an XML document.
 SAX Parser won’t create the parse tree
 Working in Element Wise: DOM works as document. SAX works on each
element of the XML document sequentially.
 Steps for parsing the XML document using SAX:
1. Examine the XML document and checks for the errors
2. Validate the XML document using DTD Schema (in case the parse
assigned for validation)
3. Identify the type of parse used
4. Copy the XML data in to the required presentation format
 Properties of XML SAX parser:

 Content Handler Interface Methods:


1. void startDocument() − Called at the beginning of a document.
2. void endDocument() − Called at the end of a document.
3. void startElement(String uri, String localName, String qName, Attributes
atts) − Called at the beginning of an element.
4. void endElement(String uri, String localName,String qName) − Called at the
end of an element.
5. void characters(char[] ch, int start, int length) − Called when character data
is encountered.
6. void ignorableWhitespace( char[] ch, int start, int length) − Called when a
DTD is present and ignorable whitespace is encountered.
7. void processingInstruction(String target, String data) − Called when a
processing instruction is recognized.
8. void setDocumentLocator(Locator locator)) − Provides a Locator that can be
used to identify positions in the document.
9. void skippedEntity(String name) − Called when an unresolved entity is
encountered.
10. void startPrefixMapping(String prefix, String uri) − Called when a new
namespace mapping is defined.
11. void endPrefixMapping(String prefix) − Called when a namespace definition
ends its scope.
 Example Program:
input.xml:
<?xml version = "1.0"?>
<class>
<student rollno = "393">
<firstname>dinkar</firstname>
<lastname>kad</lastname>
<nickname>dinkar</nickname>
<marks>85</marks>
</student>
<student rollno = "493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>vinni</nickname>
<marks>95</marks>
</student>
<student rollno = "593">
<firstname>jasvir</firstname>
<lastname>singn</lastname>
<nickname>jazz</nickname>
<marks>90</marks>
</student>
</class>
demo.java
import java.io.File;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class demo {
public static void main(String[] args) {
try {
File inputFile = new File("input.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
UserHandler userhandler = new UserHandler();
saxParser.parse(inputFile, userhandler);
} catch (Exception e) {
e.printStackTrace();
}
}
}

class UserHandler extends DefaultHandler {


boolean bFirstName = false;
boolean bLastName = false;
boolean bNickName = false;
boolean bMarks = false;
public void startElement( String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
if (qName.equalsIgnoreCase("student")) {
String rollNo = attributes.getValue("rollno");
System.out.println("Roll No : " + rollNo);
} else if (qName.equalsIgnoreCase("firstname")) {
bFirstName = true;
} else if (qName.equalsIgnoreCase("lastname")) {
bLastName = true;
} else if (qName.equalsIgnoreCase("nickname")) {
bNickName = true;
}
else if (qName.equalsIgnoreCase("marks")) {
bMarks = true;
}
}
public void endElement(String uri, String localName, String qName) throws
SAXException {
if (qName.equalsIgnoreCase("student")) {
System.out.println("End Element :" + qName);
}
}
public void characters(char ch[], int start, int length) throws SAXException {
if (bFirstName) {
System.out.println("First Name: " + new String(ch, start, length));
bFirstName = false;
} else if (bLastName) {
System.out.println("Last Name: " + new String(ch, start, length));
bLastName = false;
} else if (bNickName) {
System.out.println("Nick Name: " + new String(ch, start, length));
bNickName = false;
} else if (bMarks) {
System.out.println("Marks: " + new String(ch, start, length));
bMarks = false;
}
}
}
 SAX better than DOM:
1. It parses the XML file as a stream rather than allocating RAM for the
complete file.
2. It uses less memory and is faster than the DOM Parser because the
complete file is not stored in memory.
3. It is the best method for parsing the XML file
 Drawbacks of SAX
1. Processing all the elements in sequential order.
XPath
 XPath is the major element in XSLT.
 Purpose: It is used to navigate through elements and attributes in XML
document. (it is used to select the node (element) in XML document)
 Relationship between XSLT and XPath

 Characteristic of XPath:
1. It is a syntax for defining the parts of the XML document
2. It defines the Path expression to navigate XML documents
3. It contains standard library functions
4. It is recommend by W3C
 XPath Expressions:
1. Node-name select all the nodes with the given name
2. /  start to select from the root node
3. //  select the node from the current node that match the selection
4. .  select the current node
5. ..  select the parent of the current node
6. @  select the attributes
7. Example select all the nodes with the name Example
8. Class/student select all the student elements that are the children of
class
9. //student select all the student nodes
 Example Program:
Testxsl.xml:
<?xml-stylesheet type="text/xsl" href="testxsl.xsl"?>
<sports>
<game>
<name> cricket</name>
<players> 11 </players>
<like> India </like>
</game>
<game>
<name> Kabadi </name>
<players> 7 </players>
<like> India </like>
</game>
<game>
<name> Volleyball</name>
<players> 6 </players>
<like> USA </like>
</game>
</sports>
Testxsl.xls
<xsl-stylesheet version="1.0">
<html>
<head> </head>
<body>
XSL Transforamtion Example
<table border="1">
<tr> <td>Name </td> <td> No. of Players </td> <th>Country </td> </tr>
<xsl:for-each select="/sports/game">
<tr>
<td> <xsl:value-of-select="name"></td>
<td> <xsl:value-of-select="players"></td>
<td> <xsl:value-of-select="like"></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
XQuery
 Purpose: It is sued to query the XML document
 Characteristics of XQuery:
1. It is the language for querying XML data
2. It is similar to SQL for databases
3. It is built on XPath expressions
4. It is supported by all major databases
5. It is recommended by W3C
 Application Area of XQuery:
1. Extract information to use in a Web Service
2. Generate summary reports
3. Transform XML data to XHTML
4. Search Web documents for relevant information
 Example Program:
Accessing the value of XML file using PHP:
<?php
$l=simplexml_load_file("test.xml") or die("Failed to load");
foreach($l->children() as $t)
{
echo $t->test."<br>";
echo $t->name."<br>";
}
?>

You might also like