【发布时间】:2018-03-12 21:55:32
【问题描述】:
我正在尝试重置节点的值,但由于某种原因,更改没有反映出来。我通过XPATH 获得标签,但它没有设置我给出的值。
reqXML 是 XML 文件
我的代码
public static String changeProductVersion(String reqXML) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document document = null;
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource source = new InputSource();
source.setCharacterStream(new StringReader(reqXML));
document = builder.parse(source);
XPath xPath = XPathFactory.newInstance().newXPath();
Element element = (Element) xPath.evaluate(NYPG3Constants.NY_PG3_RL, document, XPathConstants.NODE);
if(element != null) {
element.setTextContent("17.1.0");
System.out.println(element.getTextContent());
}
} catch (Exception ex) {
ex.printStackTrace();
}
return reqXML;
}
提前致谢
【问题讨论】:
-
XML文件的结构是什么,
NYPG3Constants.NY_PG3_RL常量的值是多少?您只是在代码中设置节点的值还是要将其保存在文件中?如果要保存,这段代码中没有说明。 -
这是我的 XPATH,我想设置 17.1.0 而不是 14.0.0