The Simple API for XML (SAX)is available with the Java API for XML Processing (JAXP). Along with the Document Object Model, SAX is one of two common ways to write software that accesses XML data
DOM and SAX can either be a validating or a non validating parser.
A validating parser checks the XML file against the rules imposed by DTD or XML Schema.
A non validating parser doesn’t validate the XML file against a DTD or XML Schema.
Both Validating and non validating parser checks for the well formedness of the xml document
DOM: DOM will read the entire XML file and keep it in the memory as a tree. I wouldnt recomend DOM to be used where you are parsing realy huge XML files or XML Streams. The API is easier than SAX.. but what the heck. Generally as a rule of thumb use DOM when you want to manipulate the document, traverse the document back and forth and when they are small XML files
File file = new File(“C://TestXml.xml”);
//hook up your file here… this is a bad way to do it. Specify the path in a properties file and then read it from there. Can you make sure your code runs the same on Unix and Windows ?try {
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
//rings bells ? Factory design pattern…
Document doc = builder.parse(file); // this is what will hold a handle to
all the elements in the tree.NodeList nodes = doc.getElementsByTagName(“person”); //the tag whose
subtags you want to readfor (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList title = element.getElementsByTagName(“firstname”);
//the sub tag
Element line = (Element) title.item(0);
}
You can loop in this manner and walk throught the nodes available
SAX
Generally (yeah, am taking my arse to a safer stand
), use SAX where there are no structural modification and they are huge XML files
Pretty obvious :
XMLReader parser =
XMLReaderFactory.createXMLReader(“org.apache.xerces.parsers.SAXParser”);
org.xml.sax.ContentHandler handler = new SaxParserClassName();
//your class name
parser.setContentHandler(handler);
InputStream in = connection.getInputStream();
InputSource source = new InputSource(in);
parser.parse(source);
//parser.parse(new InputSource(“C://TestXml.xml”)); either ways !!
The comments that appear below a line, are meant for that line, below which the comments appear. The Formatting is screwing up my effort. Aiway, I think the one who is looking for help will be able to figure out what I am trying to say.