Before studying this article, please read another article in my article. JAVA 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;/** * Use dom4j to operate xml * Operation on xml attributes* Time: October 2, 2016* The operation xml file is contact.xml * The file name of this file is: attrDemo.java * @author shutu008 * */public class attrDemo{ @Test public void exmpl() throws Exception{ //Read the XML file and obtain the document object SAXReader reader = new SAXReader(); Document document = reader.read(new File("./src/contact.xml")); //Get the attribute object of a node Element rootElem = document.getRootElement(); //Get the root node attribute object Attribute rootAttr = rootElem.attribute("id"); //Get the specified node attribute object Element contactElem = rootElem.element("contact"); Attribute contactAttr = contactElem.attribute("id"); //Transfuse all attributes of a node for (Iterator it = contactElem.attributeIterator();it.hasNext();){ Attribute conAttr= (Attribute)it.next(); String conTxt = conAttr.getValue(); String conAttrName = conAttr.getName(); System.out.println(conAttrName+" = "+conTxt); } //Set the attributes and values of a node contactElem.addAttribute("name", "zhangsan"); //Set (change) the value of a certain attribute Attribute nameAttr = contactElem.attribute("name"); nameAttr.setValue("lisi"); //Delete the specified attribute of a certain node contactElem.remove(nameAttr); //Write the attributes and values of a certain node into the XML document XMLWriter writer = new XMLWriter(new FileWriter("./src/contact.xml")); writer.write(document); writer.close(); /** * If there is Chinese in the document, you need to set 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 attribute name of the specified object System.out.println(rootAttr.getName()); System.out.println(contactAttr.getName()); //Get the attribute value of the specified object System.out.println(contactAttr.getValue()); System.out.println(rootAttr.getValue()); }}Note: The above example code can be run directly. You can use Junit 4 to adjust the code of this example.
The following is the XML document:
<?xml version="1.0" encoding="UTF-8"?><contactList id="0"> <contact id="001"> <name>Zhang San</name> <age>20</age> <phone>134222223333</phone> <email>[email protected]</email> <qq>432221111</qq> </contact> <contact id="002"> <name>Li Si</name> <age>20</age> <phone>134222225555</phone> <email>[email protected]</email> <qq>4322222222</qq> </contact> <contactTwo> <name>Wang Wu</name> <age>32</age> <phone>465431341</phone> <emali>[email protected]</emali> <qq>46164694</qq> </contactTwo> <test>Test</test> <test>Other uses</test></contactList>
The file directory is shown in the figure:
The above is the full content of Java's implementation methods for adding, deleting, modifying and checking XML node attributes brought to you by the editor. I hope everyone will support Wulin.com more~