Many friends have a cold on DOM, here I spend some time to write a small tutorial, this can understand, will operate, I believe that the DOM-based other API (such as JDOM,DOM4J, etc.) generally no problem.
After the Java code , you can also download (click here to download) to import into Eclipse or MyEclipse.
The relationship between node and element
- Element is a sub-interface of node, so the method of element is more than the node method, so it is more convenient to use, the general situation we have to convert the node into an element (or called a tag, that is, element);
- Element is a sub-type of node:
For example, we run an instance of the Readbynode (node node) method, if the type is broken by code if (Node.getnodetype () ==node. Element_node), you will see the following output:
Node Name: #document节点类型: 9 nodes value: null node Name: Students node Type: 1 node value: null node Name: #text节点类型: 3 Node Value: Node Name: Student node Type: 1 node value: null node name: # Text node Type: 3 node Value:------Part of the console content has been omitted------
The output includes the text type (the DOM will also use spaces as the type) and the document type and element type, and if you only look for the element type, you can determine the node type, which we must pay attention to!
We can see from the results that the element is actually a value in "<>".
The node type constant field value can be viewed against the node "constant field value" in the Java API documentation:
The XML file needs to be updated with transformer after you modify it
Because DOM modifications are modified in memory, to be updated into an XML file, you must write using transformer. In addition to reading, other operations that add, delete, and change the updated data must be transformer updated to the XML file.
When do I use item (0)?
When we use a collection of child elements obtained through getElementsByTagName, we can use item (0) If there is only one element in the set of child elements, and of course we specify item (0) because item (0) is the first element in the first element collection.
How do I delete a node?
The first thing to get the node to delete, and then get its parent node, and then use the parent node's RemoveChild method to remove the node to delete, so delete is not "suicide".
How do I create a node?
First use the DOM's CreateElement method to create the individual elements, and then through the AppendChild method to establish a parent-child relationship between each other, the relationship does not have to start from the root node, depends on how you increase, and whether to find the exact insertion position.
-----------------------------------the following XML code -----------------------------------
<?xml version= "1.0" encoding= "Utf-8"? ><students><student id= "001" sex= "male" ><name> Stephen Chow </ Name><age>23</age><intro> This is a very good student </intro></student><student id= "002" sex= "Male" ><name> Andy Lau </name><age>32</age><intro> His comprehensive ability is excellent </intro></student ><student id= "003" sex= "Women" ><name> Zhou Huimin </name><age>31</age><intro> Beautiful </ Intro></student><student id= "004" sex= "male" ><name> Harry </name><age>37</age> <intro> score a little bad </intro></student><student id= "005" sex= "male" ><name> Zhang Sanfeng </name>< Age>26</age><intro> often skipping classes </intro></student></students>
-----------------------------------the following Java code -----------------------------------
Package Com.xmltest;import Java.io.file;import Java.io.ioexception;import javax.xml.parsers.documentbuilder;import Javax.xml.parsers.documentbuilderfactory;import Javax.xml.parsers.parserconfigurationexception;import Javax.xml.transform.outputkeys;import Javax.xml.transform.transformer;import Javax.xml.transform.transformerexception;import Javax.xml.transform.transformerfactory;import Javax.xml.transform.dom.domsource;import javax.xml.transform.stream.streamresult;//Be careful not to lead the wrong package import Org.w3c.dom.document;import Org.w3c.dom.element;import Org.w3c.dom.node;import Org.w3c.dom.NodeList;import org.xml.sax.saxexception;/** * <b> Project:</b> using DOM for CRUD (add-and-revise) operations on XML documents <br/> * <b> file name: </b > xml_crud.java<br/> * <b> class name:</b> xml_crud<br/> * <b> package:</b> com.xmltest<br/ > * <b> Description:</b> An XML simple crud operation <br/> * Reprint please note the source or author, thank you!
Use DOM for XML document CRUD (add-and-revise) operations < operation details >