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 ~