Although the class XMLProperties.java in Jive is very useful, it does not support attributes, like the sample.xml file below.
How to change the capacity of "disk D" to 30G? Also use the general recursive traversal? I heard that the latest version of JDOM has joined the support for XPath, quickly download, test! XML Version = "1.0" encoding = "UTF-8"?> 8G capacity> 200 Directories> 1580 Files> disk> 10g capacity> 500 Directories> 3000 files> disk> hd> import java.io *; import org.jdom. *; import org.jdom.input. *; import org.jdom.output. *; import org.jdom.xpath. *; public class xpathtest {public static Void main (string [] args) throws ioException, jdomexception {if (args.length! = 1) {system.err.println ("usage: java xpathtest sample.xml); return;} string filename = args [0] ; PrintStream out = System.out; SAXBuilder builder = new SAXBuilder (); Document doc = builder.build (new File (filename)); XPath capacityXPath = XPath.newInstance ( "/ HD / disk [@ name = 'D'] / CAPA city "); Object capacity = capacityXPath.selectSingleNode (doc); Element ca = (Element) capacity; ca.setText (" 30G "); XMLOutputter outputter = new XMLOutputter (" ", false); outputter.setEncoding (" gb2312 " Outputter.output (DOC, OUT);}} The following is the running result of the program, really cool! C: / java> Java Xpathtest Sample.xml <