The implementation method of Java to check and delete XML node attributes _java

Source: Internet
Author: User
Tags xml attribute

See my other article before you learn this article Java operations on XML nodes can have a better understanding of XML operations.

Package vastsum;
Import Java.io.File;
Import Java.io.FileWriter;

Import Java.util.Iterator;
Import Org.dom4j.Attribute;
Import org.dom4j.Document;
Import org.dom4j.Element;
Import Org.dom4j.io.SAXReader;
Import Org.dom4j.io.XMLWriter;

Import Org.junit.Test; /** * Using DOM4J to manipulate XML * to XML attribute operation * Time: October 2, 2016 * operation XML file is contact.xml * This file name is: Attrdemo.java * @author shutu008 * * /public class attrdemo{@Test public void Exmple () throws exception{//read XML file, get Document object Saxreader reader = new
  Saxreader ();
  
  Document document = Reader.read (New File ("./src/contact.xml"));
  Gets the attribute object Element Rootelem = Document.getrootelement () of a node; 
  
  Gets the root Node Property object attribute Rootattr = Rootelem.attribute ("id");
  Gets the specified node Property object Element Contactelem = rootelem.element ("contact");
  
  Attribute contactattr = Contactelem.attribute ("id"); Iterate through all the attributes for a node for (iterator it = Contactelem.attributeiterator (); It.hasnext ();) {attribute conattr= It.nex
   T (); String Contxt = conattr. GetValue ();
   String conattrname = Conattr.getname ();
  System.out.println (conattrname+ "=" +contxt);
  
  //Set the properties and values of a node Contactelem.addattribute ("name", "Zhangsan");
  Set (change) The value attribute of a property nameattr = Contactelem.attribute ("name");
  
  Nameattr.setvalue ("Lisi");
  Deletes the specified attribute of a node contactelem.remove (nameattr);
  Writes a node's properties and values to an XML document XMLWriter writer = new XMLWriter (New FileWriter ("./src/contact.xml"));
  Writer.write (document);
  
  Writer.close ();
   /** * If the document has Chinese need to set the character encoding * Use the following statement: * OutputFormat format = Outputformat.createprettyprint ();
   * Format.setencoding ("GBK");
   * XMLWriter writer = new XMLWriter (New FileWriter ("./src/contact.xml"), format);
  *//Get the property name System.out.println (Rootattr.getname ()) of the specified object;
  System.out.println (Contactattr.getname ());
  Gets the property value System.out.println (Contactattr.getvalue ()) of the specified object;
 System.out.println (Rootattr.getvalue ()); }
}

Note: The above example code can be run directly. You can use JUnit 4 to adjust the code for this example.

The following is an XML document:

<?xml version= "1.0" encoding= "UTF-8"?> <contactlist id= "
0" > <contact id= "001"
 class= "style" >
  <name> John </name>
  <age>20</age>
  <phone>134222223333</phone>
  <email>zhangsan@qq.com</email>
  <qq>432221111</qq>
 </contact>
 <contact id= "002" >
  <name> Dick </name>
  <age>20</age>
  <phone> 134222225555</phone>
  <email>lisi@qq.com</email>
  <qq>432222222</qq>
 </contact>
 <contactTwo>
  <name> Harry </name>
  <age>32</age>
  <phone>465431341</phone>
  <emali>af@qq.com</emali>
  <qq>46164694 </qq>
 </contactTwo>
 <test> test </test>
 <test> other uses </test>
</contactList>

The file directory is as shown in the figure:

The above is a small series for everyone to bring the Java to the XML node attributes of additions and deletions to achieve the full content of the method, I hope that we support the cloud-Habitat Community ~

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.