我正在尝试使用java中的XPath更改XML文件的特定标记的值。我正在尝试使用XpathFactory来实现这一点,但却无法实现。如果有更好的方法,请纠正我。
JAVA代码..
public class MavenMetadataReader {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws XMLStreamException {
Scanner user_input = new Scanner( System.in );
String updated_pom_version;
System.out.println("Enter updated version:");
updated_pom_version = user_input.next( );
File xpath=new File("D:\\Lucy\\trunk\\pom.xml");
XPathFactory xfactory = XPathFactory.newInstance();
XPath xpathObj = xfactory.newXPath();
Node node;
try {
node = (Node)xpathObj.evaluate(xpath, doc, XPathConstants.NODE);
} catch (XPathExpressionException e) {
throw new RuntimeException(e);
}
node.setTextContent(elementValue);
XML文件..
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.avocent</groupId>
<artifactId>common-configurations</artifactId>
<version>0.0.4</version>
</parent>
<groupId>com.avocent.commonplatform.cps.symbols</groupId>
<artifactId>GDDResources</artifactId>
<version>4.0.0.129-SNAPSHOT</version>
<description>Resources for init data strings</description>
<packaging>jar</packaging>
<properties>
<src>${basedir}</src>
<dst>${basedir}/target/classes</dst>
</properties>
</project>
XML文件中的预期更改。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.avocent</groupId>
<artifactId>common-configurations</artifactId>
<version>0.0.4</version>
</parent>
<groupId>com.avocent.commonplatform.cps.symbols</groupId>
<artifactId>GDDResources</artifactId>
<version>3.6.10</version> **<-- Changes are to be made in second version tag, not the first**
<description>Resources for init data strings</description>
<packaging>jar</packaging>
<properties>
<src>${basedir}</src>
<dst>${basedir}/target/classes</dst>
</properties>
</project>
输入的用户值应仅重写第二个“version”标记的值。请帮助
答案 0 :(得分:4)
试试这个:
try {
// Create a document by parsing a XML file
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new File("C:/temp/test.xml"));
// Get a node using XPath
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/project/version";
Node node = (Node) xPath.evaluate(expression, document, XPathConstants.NODE);
// Set the node content
node.setTextContent("Whatever I want to write");
// Write changes to a file
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(new File("C:/temp/test-updated.xml")));
} catch (Exception e) {
// Handle exception
}
为了完成上述代码,您必须使用以下导入:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;