Use DOM for XML document CRUD (add-and-revise) operations < operation Details >

Source: Internet
Author: User

Tag: blank target color style title

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.

650) this.width=650; "title=" image "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M01/54/34/wKiom1R787_ Yashuaaytdthk5gs853.jpg "width=" 1370 "height=" "/>"

The relationship between node and element
    1. 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);

      650) this.width=650; "title=" image "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/54/34/wKiom1R787_ Ynf9yaalvmlt6xs4344.jpg "width=" 1099 "height=" 377 "/>

    2. 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:

650) this.width=650; "title=" image "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/54/34/ Wkiom1r788ccoazeaanjqvf3aac403.jpg "width=" 469 "height=" 463 "/>

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?

First to get the node to be deleted, and then get its parent node, and then use the parent node's RemoveChild method to delete the node to delete, so delete is not "suicide" 650) this.width=650; "Class=" Wlemoticon Wlemoticon-openmouthedsmile "style=" border-top-style:none; Border-bottom-style:none; Border-right-style:none; Border-left-style:none "alt=" laughs "src=" http://s3.51cto.com/wyfs02/M01/54/33/wKioL1R79EnRucv6AAAHPN_3sdE907.jpg "/ >.

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/> *  XML crud additions and deletions   * 

Reprint please note the source or author, thank you!

Use DOM for XML document CRUD (add-and-revise) operations &lt; operation Details &gt;

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.