Java XML parsing DOM, javaxmldom

Source: Internet
Author: User

Java XML parsing DOM, javaxmldom
DOM Parsing is one of the official XML parsing methods. You do not need to introduce a third-party package when using it. The code is easy to write and the tree structure is easy to modify, however, since DOM parsing loads the entire XML file into the memory for parsing, when the XML file is large, the efficiency of DOM Parsing is reduced and may cause memory overflow. When the XML file is large, it should be parsed using SAX (which will be introduced in the next article ). Generally, if the XML file is only used as the system configuration file, the file will not be very large, and DOM parsing can basically solve the problem. The basic steps for parsing and generating xml are as follows: 1. XML parsing: As mentioned above, DOM parsing loads the entire XML file into the memory. Therefore, the first step of parsing is to load the file. You need to use the following code:

1 DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); 2 DocumentBuilder builder = factory. newDocumentBuilder (); 3 Document document = builder. parse (in); // There are multiple types of parameters. in this example, the InputStream type parameter is selected.

These three lines of code are used to create a parser factory, create a parser, and obtain the parsed XML document tree. Then, you can perform specific operations on the document object.

The main operations are as follows:
1 Element root = document. getDocumentElement (); // get the document's root Node 2 NodeList nodeList = document. getElementsByTagName ("nodename"); // obtain the node list based on the node name

Then, the nodes in nodeList are retrieved and the subnode list is obtained using the getChildNodes () method of the Node object until the desired node is obtained.

Note: Dom parsing regards all the content between the two labels in xml as subnodes, including spaces. The node types are as follows:
1. The child nodes with tags are considered as child nodes of the Element type. 2. Blank child nodes without tags or text nodes are considered as child nodes of the text type.
When obtaining the node value, you must obtain the value of a text node, instead of the value of an Element node. The value of an Element node is always null. You can also use getTextContent () of an Element node () to obtain the node value of the Element type. Therefore, nodeList is required when traversing the child node. item (I ). getNodeType () = Element. if ELEMENT_NODE is set to true, nodeList is executed. item (I ). getTextContent () to get the node value. 2. XML generation: first, create a Document Object and use the following code:
1 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();2 DocumentBuilder builder = factory.newDocumentBuilder();3 Document document = builder.newDocument();

Then configure the document Object and create and add nodes:

1 document. setXmlStandalone (true); // optional. After configuration, you can remove unnecessary standalone attributes 2 Element root = document. createElement ("nodes"); // create document root node 3 document. appendChild (root); // Add the root node to the document

Create a child node, add the id attribute, and add it to the root node. The parent-child relationship of nodes in xml is formed by the node appendChild method.

1 Element node = document. createElement ("node"); // create a subnode 2 node. setAttribute ("id", "1"); // Add attribute 3 root for the child node. appendChild (node); // Add the child node to the root node

After the node is set, you need to convert the document object to an xml file by using the following code:

1 TransformerFactory factory = TransformerFactory. newInstance (); // factory class, used to obtain the conversion object 2 Transformer transformer = factory. newTransformer (); // obtain the object 3 transformer for conversion. setOutputProperty (OutputKeys. INDENT, "yes"); // sets the automatic line feed of the document 4 transformer. transform (new DOMSource (document), new StreamResult (new File ("E: \ nodes. xml "); // to achieve conversion, You need to wrap the conversion source object and target file
Now, the XML file is generated through DOM. The basic steps for DOM parsing and XML creation are as many as above. This is my summary and I hope to help anyone who needs to learn this knowledge.

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.