SAX parsing is event based modelling.When a Sax parser parses a XML document, it generates call back events as it encounters start-tags, end-tags, text, comments and so on. The class DefaultHandler is the base class to listen such events. It contains methods to handle respective events. For example,
when it encounters a Start Tag it calls method
public void startElement()
public void startElement()
when it encounters a End Tag it calls this method
public void endElement()
public void endElement()
Source Code :
package com.manoj.examples.xml; import java.io.FileReader; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; /** * The Class XMLParserSAX. * @author Manoj Shrestha * @contact m.javaprogrammer@gmail.com */ public class XMLParserSAX extends DefaultHandler { private static final String QUOTE = "\""; public static void main(String args[]) throws Exception { try { // get a new instance of parser SAXParser sp = SAXParserFactory.newInstance().newSAXParser(); // parse the file and also register this class for call backs // The file name is relative to the main project folder sp.parse("Personnel.xml", new XMLParserSAX()); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // Another way to parse XML through XMLReader /** FileReader fileReader = new FileReader("Personnel.xml"); XMLReader xr = XMLReaderFactory.createXMLReader(); xr.setContentHandler(new XMLParserSAX()); xr.parse(new InputSource(fileReader)); **/ } @Override public void startDocument() { System.out.println("Start document"); } @Override public void endDocument() { System.out.println("End document"); } @Override public void startElement(String uri, String name, String qName, Attributes atts) { if ("".equals(uri)) System.out.print("Start element: " + qName); else System.out.print("Start element: {" + uri + "}" + name); if (atts.getLength() > 0) { System.out.print(" Attributes : "); for (int i = 0; i < atts.getLength(); i++) { System.out.print(atts.getLocalName(i) + " = " + QUOTE + atts.getValue(i) + QUOTE + " "); } } System.out.println(); } @Override public void endElement(String uri, String name, String qName) { if ("".equals(uri)) System.out.println("End element: " + qName); else System.out.println("End element: {" + uri + "}" + name); } @Override public void characters(char ch[], int start, int length) { System.out.print("Characters: \""); for (int i = start; i < start + length; i++) { switch (ch[i]) { case '\\': System.out.print("\\\\"); break; case '"': System.out.print("\\\""); break; case '\n': System.out.print("\\n"); break; case '\r': System.out.print("\\r"); break; case '\t': System.out.print("\\t"); break; default: System.out.print(ch[i]); break; } } System.out.print("\"\n"); } } |
The input file : “Personnel.xml”
<?xml version="1.0" encoding="UTF-8"?> <Personnel> <Employee type="permanent" id="p124"> <Name>Manoj Shrestha</Name> <Age>25</Age> <Address>Tokyo</Address> </Employee> <Employee type="contract" id="c230"> <Name>Lionel Messi</Name> <Age>24</Age> <Address>Barcelona</Address> </Employee> </Personnel> |
The console output :
Start document Start element: Personnel Characters: "\n\t" Start element: Employee Attributes : type = "permanent" id = "p124" Characters: "\n\t\t" Start element: Name Characters: "Manoj Shrestha" End element: Name Characters: "\n\t\t" Start element: Age Characters: "25" End element: Age Characters: "\n\t\t" Start element: Address Characters: "Tokyo" End element: Address Characters: "\n\t" End element: Employee Characters: "\n\t" Start element: Employee Attributes : type = "contract" id = "c230" Characters: "\n\t\t" Start element: Name Characters: "Lionel Messi" End element: Name Characters: "\n\t\t" Start element: Age Characters: "24" End element: Age Characters: "\n\t\t" Start element: Address Characters: "Barcelona" End element: Address Characters: "\n\t" End element: Employee Characters: "\n" End element: Personnel End document |
No comments :
Post a Comment