xml is a document and it carries data in the form of xml tags. Initially java was supporting IO programming and no xml data readers. In those days programmer is responsible to create their own program to read and process the xml files. So many third party libraries has come to market but these may cost in future. Sun Micro System has decided to release standard JAX-P for working with xml documents
Start Document: This will be fired at the start of the document.
Start Element: When ever parser encounters start element, this method is called.
characters: This will execute when data portion is encounters in the xml file.
End Element: Encounters at the end of the xml element
End Document: End of the document
Main method to read the data
public class EmployeeSAXExample
{
public static final String classpath = System.getProperty("java.class.path");
public static final String pathSeparator = System.getProperty("path.separator");
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
System.out.println(classpath);
parser.parse(new File(classpath + "/com/ibm/pdc/ephs/jaxp/resources/Product.xml"), new EmployeeDefaultHandler());
}
}
public class EmployeeDOMExample
{
public static final String classpath = System.getProperty("java.class.path");
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(classpath + "/com/ibm/pdc/ephs/jaxp/resources/Employee.xml");
System.out.println(doc.getFirstChild().getNodeName());
System.out.println(doc.getFirstChild().getFirstChild().getNextSibling().getNodeName());
}
}
- Jax-P stands for Java API for XML processing
- Two xml processing methodologies
- DOM (Document Object Model)
- SAX (Sample Access for XML)
SAX (Simple Access for XML)
- Reads xml files in event based model
- This model has 3 things
- Source
- Listener
- Event Handler
- SAX is a sequential processing model
- SAX is fast compare with DOM
- It consumes less memory
- SAX is read only API
- Main interface is SAXParser
- Main Class is SAXParserFactory
Source:(XML)
It is the place or originator of events. It rises the events like mouse over, key pressed, AWT like event
Listener:(Parser)
It listens the events means catch the events which were raised on Source. It will handover events to Handlers.
Event Handlers:(Handler)
Listener handover the event to event handler and this one is holding separate methods to handle every event.
Source is the xml file which rises several events
Parser is the listener and it will reads the events and handover them to handler
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
Handler class example:
package com.ibm.pdc.ephs.jaxp;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class EmployeeDefaultHandler extends DefaultHandler
{
@Override
public void startDocument() throws SAXException
{
super.startDocument();
System.out.println("Reading of document is started.");
}
@Override
public void endDocument() throws SAXException
{
super.endDocument();
System.out.println("Documenet reading is completed.");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
super.startElement(uri, localName, qName, attributes);
System.out.print("<" + qName + ">");
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
super.endElement(uri, localName, qName);
System.out.print("</" + qName + ">");
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
super.characters(ch, start, length);
String data = new String(ch, start, length);
System.out.print(data);
}
}
Start Document: This will be fired at the start of the document.
Start Element: When ever parser encounters start element, this method is called.
characters: This will execute when data portion is encounters in the xml file.
End Element: Encounters at the end of the xml element
End Document: End of the document
Main method to read the data
public class EmployeeSAXExample
{
public static final String classpath = System.getProperty("java.class.path");
public static final String pathSeparator = System.getProperty("path.separator");
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
System.out.println(classpath);
parser.parse(new File(classpath + "/com/ibm/pdc/ephs/jaxp/resources/Product.xml"), new EmployeeDefaultHandler());
}
}
DOM (Document Object Model):
- Reads the data in the from of Hierarchical model. It is like parent and child depicts tree like structure.
- When xml given to DOM, it will load entire xml into memory at single time.
- DOM is read or write model
<mobile>
<name></name>
<model></model>
<company>
<name></name>
<owner></owner>
</compary>
</mobile>
Mobile
|
|
|-------------<name>
|
|-------------<model>
|
|
|-------------<company>
|
|----------<name>
|
|----------<owner>
<name></name>
<model></model>
<company>
<name></name>
<owner></owner>
</compary>
</mobile>
Mobile
|
|
|-------------<name>
|
|-------------<model>
|
|
|-------------<company>
|
|----------<name>
|
|----------<owner>
{
public static final String classpath = System.getProperty("java.class.path");
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(classpath + "/com/ibm/pdc/ephs/jaxp/resources/Employee.xml");
System.out.println(doc.getFirstChild().getNodeName());
System.out.println(doc.getFirstChild().getFirstChild().getNextSibling().getNodeName());
}
}
No comments:
Post a Comment