Difference between DOM vs SAX Parser in Java - XML Parsing in Java

DOM vs SAX parser in Java
DOM and SAX parser are the two most popular parsers used in the Java programming language to parse XML documents. DOM and SAX concept is originally XML concept and Java programming language just provide an API to implement these parsers. Despite both DOM and SAX are used in XML parsing, they are completely different from each other. In fact difference between DOM and SAX parser is a popular Java interview question asked during Java and XML interviews. DOM and SAX parser has a different way of working, which makes Java programmer understand the difference between DOM and SAX parser even more important.

JDOM Example : Reading and Parsing XML with SAX parser in Java

XML parsing with JDOM parser
JDOM is an open source library which allow XML parsing and reading in Java program. JDOM is designed by using Java programming technique and customized for Java programmers, so that Java programmer with  very little knowledge of XML documents can use JDOM to read XML files. Unlike DOM Parser of Java API , which uses Factory design pattern to create instance of parser e.g DocumentBuilderFactory and DocumentBuilder, as seen in our last example of parsing XML documents in Java, JDOM uses new() operator to create its parser instances. In fact JDOM is very easy to understand and most of the time its self explanatory. 

XML Interview questions and answers for programmer

XML Interview questions and answers for programmer
---------------------------------------------------
What is XML ?
Difference between DTD and XML Schema?
What is Xpath ?
What is XSLT?
What is element and attribute in XML?
What is meaning of well formed XML ?
What is benefit of using XML ?
What is XML namespace? Why it's important?
Difference between DOM and SAX parser ?
What is a CDATA section in XML?
What is SOAP and how does it relate to XML?
What is XML data Binding ?
What is XML binding ? How to you bind XML document into Java objects?
What is XML Parsing? Which XML parsers have you used ?
What is difference between DOM and SAX parsers?
What is XPATH ? Can you run XPATh expression in Java ? How?
Does Java supports XSLT? How will you do XSLT transformation in Java?
How to write xsl stylesheet to remove an attribute from xml document?
Which Java open source library have you used for XML processing?
What is xml beans? have you used it?
Which packages are related to XML functionalities in Java?
What is difference between DTD and Schema?
What are xml namespaces? how do you resolve conflicts?
What are the issues you faced while doing XML processing in Java? 

XPath Example in Java

10 Example of XPATH
------------------------
package test;

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
  * Simple Java program to execute XPATH expression and retrieve value from XML documents
  */
public class XPathExample {

    public static void main(String[] args){
        try {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true);
         
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document doc = builder.parse("Books.xml");
         
         
            XPath xpath = XPathFactory.newInstance().newXPath();
         
            XPathExpression expr = xpath.compile("bookstore/book[@category='COOKING']/title/text() | bookstore/book[@category='WEB']/title/text()");

         
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
         
         
            NodeList nodes = (NodeList) result;
            System.out.println("Result: " + nodes);
            for (int i = 0; i < nodes.getLength(); i++) {
                System.out.println(nodes.item(i).getNodeValue());
            }
         
        }catch (XPathExpressionException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        }
    }
}

That's all about how to use XPath in Java.