Use DOM to perform crud (add, delete, modify, and query) operations on xml documents & lt; Operation Details & gt;, domcrud

Source: Internet
Author: User

Use DOM to perform crud (add, delete, modify, and query) operations on xml documents <Operation Details>, domcrud

Many of my friends have caught a cold in DOM. I spent some time writing a small tutorial here. I can understand and operate it. I believe other DOM-Based APIS (such as JDOM and DOM4J) generally, there is no problem.

AppendixJava codeAnd can be downloaded (you can clickHereImport and download) import to Eclipse or MyEclipse.

 

Relationship between Node and Element

For example, if we comment out the type code if (Node. getNodeType () = node. ELEMENT_NODE) by running the readByNode (node Node) method in the instance, you will find that the following content is output:

Node name: # document node type: 9 node value: null node name: students node type: 1 node value: null node name: # text node type: 3 node value: node name: student node type: 1 node value: null node name: # text node type: 3 node value: ------ some console content has been omitted ------

The output content includes the text type (space is also treated as the type in DOM), document type and Element type. If you only search for the Element type, you can determine the Node type. We must pay attention to this!

We can see from the result that the Element is actually a value in "<>.

The constant field value of the Node type can be viewed against the Node "constant field value" in the Java API documentation:

 

 

After modifying the xml file, you must use Transformer to update it to the xml file.

Because DOM modification is modified in memory, to update to an xml file, you must use Transformer to write data. In addition to reading, other operations to add, delete, and modify the updated data must be Transformer updated to the xml file.

When to use item (0 )?

When getElementsByTagName is usedSet of child elementsIf this sub-element set contains only one element, we can use item (0). Of course, we specify item (0) Because item (0) is the first element in the first element set.

How to delete a node?

First, you must obtain the node to be deleted, then obtain its parent node, and then use the removeChild method of the parent node to delete the node. Therefore, deleting a node cannot be "killed ".

How to Create a node?

First, use the createElement method of DOM to create each element, and then use the appendChild method to establish a parent-child relationship between each element. This relationship does not have to start from the root node. It depends on how you increase it, you can find the insert position accurately.

 

-----------------------------------The following is the xml Code-----------------------------------

 

<? Xml version = "1.0" encoding = "UTF-8"?> <Students> <student id = "001" sex = "male"> <name> Stephen Chow </name> <age> 23 </age> <intro> good student </intro> </student> <student id = "002" sex = "male"> <name> Andy Lau </name> <age> 32 </age> <intro> excellent comprehensive capabilities </intro> </student> <student id = "003" sex = "female"> <name> Zhou Huimin </name> <age> 31 </age> <intro> pretty </intro> </student> <student id = "004" sex = "male"> <name> Wang Wu </name> <age> 37 </age> <intro> poor score </intro> </student> <student id = "005" sex = "male"> <name> Zhang Sanfeng </name> <age> 26 </age> <intro> skip classes </intro> </student> </students>

 

-----------------------------------The following is the 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. tra Nsform. dom. DOMSource; import javax. xml. transform. stream. streamResult; // do not import the 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> Use DOM to perform crud (add, delete, modify, and query) 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> A simple xml crud operation <br/> * 

Reprinted. Please pay attention to the source or author. Thank you!

Related Article

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.