Dom of Java XML parsing

Source: Internet
Author: User

Dom parsing is one of the official XML parsing methods, without introducing a third-party package, code writing is simple, easy to modify the tree structure, but because the DOM parsing is to load the entire XML file into memory for parsing, so when the XML file is large, the use of DOM parsing efficiency will be reduced, and may cause memory overflow. Sax parsing should be used when the XML file is large (described in the next article). Typically, if the XML file is only used as a system configuration file, the file is generally not very large, and the use of DOM parsing is fundamental to solving the problem. The following are the basic steps for parsing and generating XML: 1. XML parsing: The DOM parsing has been said to load the entire XML file into memory, so the first step in parsing is to load the file, using the following code:
1 documentbuilderfactory factory = documentbuilderfactory.newinstance (); 2 Documentbuilder builder = factory.newdocumentbuilder (); 3 Document document = Builder.parse (in); // There are several types of parameters, in this case, the parameters of the InputStream type are selected

These three lines of code are to create the parser factory, create the parser, get the parsed XML document tree, and then you can do specific operations on the Document object.

Some of the main operations are as follows:
1 Element root = document.getdocumentelement (); // gets the root node of the document 2 NodeList NodeList = document.getElementsByTagName ("nodename"); // get a list of nodes based on node name

Then iterate through the nodes in the nodelist, and take advantage of the node object's Getchildnodes () method to get a list of child nodes until the desired node is fetched.

Parsing Note: Dom parsing will treat everything between two tags in the XML as child nodes, including whitespace, where the node type is divided into:
1. A child node with a label is considered a child of element type 2. A blank child node that does not contain a label or a text node as a child of the text type
When you get a node value, you must get the value of the node of the text type, not the value of the element type node, the value of element node is always null, or you can use the element node's gettextcontent () to get the node value of element type. When traversing a child node, you need to doNodelist.item (i). Getnodetype () = = Element.element_nodeSuch a judgment, if true, executes theNodelist.item (i). Gettextcontent ()To get the value of the node. 2. XML generation: The first is to create a Document object, you need to use the following code:
1 documentbuilderfactory factory = documentbuilderfactory.newinstance (); 2 Documentbuilder builder = factory.newdocumentbuilder (); 3 Document document = Builder.newdocument ();

Then there is the configuration of the Document object and the creation and addition of the node:

1 document.setxmlstandalone (true// non-mandatory configuration, after configuration can remove unnecessary standalone properties 2 // Create a document root node 3 document.appendchild (root); // adding the root node to the document

Create child nodes, add ID attributes, and join the root node, the parent-child relationship of the nodes in XML is formed by the AppendChild method of the node.

1 Element node = document.createelement ("node"); // Create child nodes 2 // to add a property to a child node 3 // to join a child node to the root node

When the node setting is complete, the document object needs to be converted to an XML file, which needs to be converted by the following code:

 1  transformerfactory factory = Transformerfactory.newinstance (); //  factory class, used to get the transform object  2  Transformer Transformer = Factory.newtransformer (); //  Gets the object used for the conversion  3  Transformer.setoutputproperty (outputkeys.indent, "yes"); //  set document Wrap  4  transformer.transform (new  Domsource ( Document), new  Streamresult (new  File ("E : \\nodes.xml ")); //  to implement the conversion, you need to wrap the converted source object with the target file  
This completes the creation of the XML file through the DOM. Dom parsing and the basic steps to create XML that is so much, this is my study after the summary, I hope that the need to learn this knowledge of friends can have a little help.

Dom of Java XML parsing

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.