Add, delete, modify, and query xml node attributes in java

Source: Internet
Author: User

Add, delete, modify, and query xml node attributes in java

Before learning this article, please read my other article. JAVA provides a better understanding of XML operations on XML nodes.

1 package vastsum; 2 3 import java. io. file; 4 import java. io. fileWriter; 5 import java. util. iterator; 6 7 import org. dom4j. attribute; 8 import org. dom4j. document; 9 import org. dom4j. element; 10 import org. dom4j. io. SAXReader; 11 import org. dom4j. io. XMLWriter; 12 import org. junit. test; 13 14/** 15 * use dom4j to operate xml16 * to operate xml properties 17 * Time: October 18 * the xml file operated is contact. xml19 * the file name is attrDemo. java20 * @ author shutu00821 * 22 */23 public class attrDemo {24 @ Test25 public void exmple () throws Exception {26 // read the XML file, obtain the document Object 27 SAXReader reader = new SAXReader (); 28 Document document = reader. read (new File (". /src/contact. xml "); 29 30 // get the attribute object 31 Element rootElem = document for a node. getRootElement (); 32 // get the root node Attribute object 33 Attribute rootAttr = rootElem. attribute ("id"); 34 35 // get the attribute object 36 Element contactElem = rootElem of the specified node. element ("contact"); 37 Attribute contactAttr = contactElem. attribute ("id"); 38 39 // traverse all attributes of a node 40 for (Iterator it = contactElem. attributeIterator (); it. hasNext ();) {41 Attribute conAttr = (Attribute) it. next (); 42 String conTxt = conAttr. getValue (); 43 String conAttrName = conAttr. getName (); 44 System. out. println (conAttrName + "=" + conTxt); 45} 46 // set the attributes and values of a node 47 contactElem. addAttribute ("name", "zhangsan"); 48 49 // set (Change) the value of an Attribute 50 Attribute nameAttr = contactElem. attribute ("name"); 51 nameAttr. setValue ("lisi"); 52 53 // delete a node's specified attribute 54 contactElem. remove (nameAttr); 55 // write the attributes and values of a node to 56 XMLWriter writer = new XMLWriter (new FileWriter (". /src/contact. xml "); 57 writer. write (document); 58 writer. close (); 59 60/** 61 * If the document contains Chinese characters, you need to set the character encoding 62 * using the following statement: 63 * OutputFormat format = OutputFormat. createPrettyPrint (); 64 * format. setEncoding ("GBK"); 65 * XMLWriter writer = new XMLWriter (new FileWriter (". /src/contact. xml "), format); 66 */67 // get the attribute name of the specified object 68 System. out. println (rootAttr. getName (); 69 System. out. println (contactAttr. getName (); 70 // get the attribute value of the specified object 71 System. out. println (contactAttr. getValue (); 72 System. out. println (rootAttr. getValue (); 73} 74}

Note: The above sample code can be run directly. You can use Junit 4 to adjust the code in this example.

The following is an XML document:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <contactList id = "0"> 3 <contact id = "001" class = "style"> 4 <name> Zhang San </name> 5 <age> 20 </age> 6 <phone> 134222223333 </phone> 7 <email> zhangsan@qq.com </email> 8 <qq> 432221111 </qq> 9 </contact> 10 <contact id = "002 "> 11 <name> Li Si </name> 12 <age> 20 </age> 13 <phone> 134222225555 </phone> 14 <email> lisi@qq.com </email> 15 <qq> 432222222 </qq> 16 </contact> 17 <contactTwo> 18 <name> Wang Wu </name> 19 <age> 32 </age> 20 <phone> 465431341 </phone> 21 <emali> af@qq.com </emali> 22 <qq> 46164694 </qq> 23 </contactTwo> 24 <test> test </test> 25 <test> Other purpose </test> 26 </contactList>View Code

File directory:

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.