JDOM study (4) XPath test (transfer)

xiaoxiao2021-03-06  41

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! 8G 200 1580 10g 500 3000 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 <

转载请注明原文地址:https://www.9cbs.com/read-55974.html

New Post(0)