Summary of JAXP parsing XML documents with DOM mode

Source: Internet
Author: User
Tags abstract object model

1. XML Parsing Technology Overview
 
Parsing an XML document generally has two techniques: Dom (document Object Model) and sax (simple API for XML). DOM, the Document Object model, is a way of dealing with XML recommended by the Organization, and Sax is not an official standard, but it is the de facto standard of the XML community, which is supported by almost all XML parsers.

1.1 dom Mode

The DOM method parses the text into a Document object and parses all the elements and text into an object: The parser parses the tag into an element object, parses the content into a text object, and parses the attribute into attribute. For all nodes can be represented by the node object, when there is no appropriate method call, the node is cast to the above specific type, the above class is the child class node.
Using the DOM method to parse the XML document requires only the document object, so that all nodes can be obtained because the tree structure diagram is parsed in memory. Advantages: The realization of curd is particularly convenient and the operation speed is relatively fast. Disadvantage: If the file is large, the memory consumption is particularly large, it is very easy to cause memory overflow, so DOM is not and operation of large XML documents. 1.2 sax Mode

The Sax method is not to first parse the text into an entire object, it is to read a row to parse a row. Advantages: Fast parsing, low memory footprint, and very convenient to find data. Cons: only suitable for finding data, not suitable for curd operation.

For these two ways, there are many parsing development packages, mainly JAXP and dom4j, this article first describes how the JAXP parser parses XML documents in the DOM way.

2. JAXP parser parsing XML document with Dom method
 
The JAXP development package is part of the J2SE, which consists of Javax.xml, Org.w3c.dom, org.xml.sax packages and their child packages. In the Javax.xml.parsers package, several factory classes are defined, and the programmer calls these factory classes to get the parser object for DOM and sax parsing the XML document. The documentbuilderfactory in the Javax.xml.parsers package is used to create the parser object for the DOM pattern, Documentbuilderfactory is an abstract factory class that cannot be instantiated directly, but the class provides a static newinstance () method, which automatically creates a factory object and returns based on the parser installed by default on the local platform.
First, a book.xml document is given, as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<bookstore> 
  <book name= "Book1" password= "123" > 
    <price>59 </price>  
    <name>java God book 1^^</name>  
    <author> Nu Shen Wu </author >  
  </book> 
  <book name= "Book2" password= "123456" > 
    <price>69 dollar </price>  
    <name>java book 2^^</name>  
    <author> Nu Shen </author>  
  </book> 
</bookstore >

Let's use JAXP to specifically parse this Book.xml document: 2.1 Get the value of the node

@Test public
Void Read () throws exception{
    Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();//Get factory
    Documentbuilder builder = Factory.newdocumentbuilder ();//Generate Parser Document Document
    = Builder.parse (New File ("Src/book.xml"));//parse XML document to get document representing documents

    NodeList list = document.getElementsByTagName ("price");//Returns a collection of all price nodes node price
    = List.item (0);//Get the first price node
    String value = price.gettextcontent ();//Gets the value
    of the node System.out.println (value); 59 USD    
}
2.2 Modifying the value of a node
@Test public void Update () throws exception{documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
    Documentbuilder builder = Factory.newdocumentbuilder ();

    Document document = Builder.parse (new File ("Src/book.xml"));
    Node Price = document.getElementsByTagName ("priced"). Item (0);
    Price.settextcontent ("89 Yuan");
     The transformer class in the/*javax.xml.tranform package is used to output a Document object that represents an XML file into a format.
     * For example, after applying the stylesheet to an HTML document, using this object, you can also write the document object back into an XML file. The *transformer class completes the conversion operation through the transform (source Xmlsource, Result Outputtarget) method, which accepts a source and a destination. We can associate the document object to be converted by: * Javax.xml.transform.dom.DOMSource class, * with Javax.xml.transform.stream.StreamRes
     The Ult object to represent the destination of the data.
     *transformer abstract class, but its object is obtained by Transformerfactory.newtransformer.
    * *///Get converter factory transformerfactory tf = Transformerfactory.newinstance ();   
    Get converter Transformer ts = Tf.newtransformer (); Ts.transform (new Domsource (document), New Streamresult ("src/Book.xml "))); Convert the modified Document object (DOM) to Book.xml}
2.3 Adding a new node
@Test public
void AddNode () throws exception{
    Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
    Documentbuilder builder = Factory.newdocumentbuilder ();
    Document document = Builder.parse (new File ("Src/book.xml"));

    Create nodes that need to be incremented node price
    = document.createelement ("price");
    Price.settextcontent ("109 yuan");

    Get the parent node of the node that needs to be added nodes, parent
    = document.getElementsByTagName ("book"). Item (0);//first book node

    // Hang the node that needs to be added to the parent node
    parent.appendchild (price);

    Transformerfactory tf = Transformerfactory.newinstance ();
    Transformer ts = Tf.newtransformer ();
    Ts.transform (new Domsource (document), New Streamresult (New File ("Src/book.xml"));
}
2.4 Adding a new node to the specified location
@Test public
void Addnodewithindex () throws Exception {
    Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
    Documentbuilder builder = Factory.newdocumentbuilder ();
    Document document = Builder.parse (new File ("Src/book.xml"));

    Node node = document.createelement ("price");
    Node.settextcontent ("39 Yuan");
    Node parent = document.getElementsByTagName ("book"). Item (0);
    Parent.insertbefore (node, document.getelementsbytagname ("name"). Item (0));//Add node Transformerfactory TF before the title

    Transformerfactory.newinstance ();
    Transformer ts = Tf.newtransformer ();
    Ts.transform (new Domsource (document), New Streamresult (New File ("Src/book.xml"));
}
2.5 Delete the specified node
@Test public
Void Delete () throws exception{
    Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
    Documentbuilder builder = Factory.newdocumentbuilder ();
    Document document = Builder.parse (new File ("Src/book.xml"));

    NodeList nodes = document.getElementsByTagName ("price");//Gets all prices node for
    (int i = 0; I <= nodes.getlength (); i++) { C6/>if (Nodes.item (i). Gettextcontent (). Equals ("109 yuan")) {
            //cannot delete itself, let its parent node to delete
            Nodes.item (i). Getparentnode (). RemoveChild (Nodes.item (i));//delete all nodes priced at $109
        }
    }

    transformerfactory tf = Transformerfactory.newinstance ();
    Transformer ts = Tf.newtransformer ();
    Ts.transform (new Domsource (document), New Streamresult (New File ("Src/book.xml"));
}
2.6 Properties of the action document
 @Test public void Updateattribute () throws exception{documentbuilderfactory factory = Docume
    Ntbuilderfactory.newinstance ();
    Documentbuilder builder = Factory.newdocumentbuilder ();

    Document document = Builder.parse (new File ("Src/book.xml"));
    When manipulating XML document elements, the element is generally treated as node, but if node does not have a suitable method, it is converted to the appropriate type, node node = document.getelementsbytagname ("book"). Item (0);
    Element book = null;
    if (node.getnodetype () = = Node.element_node)//Before conversion, it is best to first determine the type of book = (ELEMENT) node;
    Book.setattribute ("name", "--book--");

    Book.setattribute ("Password", "--123--");
    Transformerfactory tf = Transformerfactory.newinstance ();
    Transformer ts = Tf.newtransformer ();
Ts.transform (new Domsource (document), New Streamresult (New File ("Src/book.xml")); }

The above is the JAXP parser using DOM method to parse the XML document, it can be seen that if read-only do not write, as long as Get parser factory –> get parser –> the XML into document, and then start to get the data you want to get. If you want to write data to an XML document, get the converter factory –> get converter –> convert the document to XML. So obviously, this is the two opposite process.

-Willing to share and progress together.
-More articles please see: http://blog.csdn.net/eson_15

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.