Java XML :DOM, SAX, JDOM,JAXB

来源:互联网 发布:数据魔方的作用 编辑:程序博客网 时间:2024/05/01 22:56

http://www.mkyong.com/tutorials/java-xml-tutorials/


In Java JDK, two built-in XML parsers are available – DOM andSAX, both have their pros and cons.

In addition, updated JAXB example to show you how to convert object to / from XML.


1.DOM XML Parser

The DOM is the easiest to use Java XML Parser.

DOM Parser is slow and consume a lot memory if it load a XML document which contains a lot of data.

DOM parser parses the entire XML document and loads it into memory; then models it in a “TREE” structure for easy traversal or manipulation.

In short, it turns a XML file into DOM or Tree structure, and you have to traverse a node by node to get what you want.

What is Node?
In the DOM, everything in an XML document is a node, read this.

    File fXmlFile = new File("/Users/xml/staff.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);


            // optional, but recommended
            // read this -
            // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
            doc.getDocumentElement().normalize();

            System.out.println("Root element :"+ doc.getDocumentElement().getNodeName());

            NodeList nList = doc.getElementsByTagName("staff");


2.SAX XML Parser

SAX parser is work differently with DOM parser,

it does not load any XML document into memory and create some object representation of the XML document.

Instead, the SAX parser use callback function (org.xml.sax.helpers.DefaultHandler) to informs clients of the XML document structure.

SAX Parser is faster and uses less memory than DOM parser.

See following SAX callback methods :

  • startDocument() and endDocument() – Method called at the start and end of an XML document.
  • startElement() and endElement() – Method called at the start and end of a document element.
  • characters() – Method called with the text contents in between the start and end tags of an XML document element.


        SAXParserFactory factory = SAXParserFactory.newInstance();
       SAXParser saxParser = factory.newSAXParser();
 

       DefaultHandler handler = new DefaultHandler() {}

       public void endElement(String uri, String localName,String qName) throws SAXException {

      }
 
    public void characters(char ch[], int start, int length) throws SAXException {
   }

   saxParser.parse("c:\\file.xml", handler);


3.JDOM XML Parser

JDOM is, quite simply, a Java representation of an XML document.

JDOM provides a way to represent that document for easy and efficient reading, manipulation, and writing.

It has a straightforward API, is a lightweight and fast, and is optimized for the Java programmer.

It’s an alternative to DOM and SAX, although it integrates well with both DOM and SAX.

JDOM, Java XML parser, is more user friendly in the way of accessing the XML document.

In this example, we show you how to use JDOM to read a XML file, and print out each element orderly.


         SAXBuilder builder = new SAXBuilder();
          File xmlFile = new File("/Users/xml/staff.xml");
    
          try {
    
            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            List list = rootNode.getChildren("staff");

 

5.JAXB, stands for Java Architecture for XML Binding,

using JAXB annotation to convert Java object to / from XML file.

In this tutorial, we show you how to use JAXB to do following stuffs :

  1. Marshalling – Convert a Java object into a XML file.
  2. Unmarshalling – Convert XML content into a Java Object.

Technologies used in this article

  1. JDK 1.6
  2. JAXB 2.0

Working with JAXB is easy, just annotate object with JAXB annotation, later usejaxbMarshaller.marshal() or jaxbMarshaller.unmarshal() to do the object / XML conversion.