使用 XPath Java 设置节点值

发布于 2024-12-21 23:22:42 字数 646 浏览 2 评论 0原文

我正在尝试通过 XPath 设置节点值。我有以下内容,但它似乎没有改变实际文件值。

XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();

xPathExpression = "//test";
xPathValue= "111";

NodeList nodes = (NodeList) xPath.evaluate(xPathExpression, new InputSource(new FileReader(fileName)), XPathConstants.NODESET);

for (int k = 0; i < nodes.getLength(); i++)
{
    System.out.println(nodes.item(k).getTextContent());  // Prints original value
    nodes.item(k).setTextContent(xPathValue);
    System.out.println(nodes.item(k).getTextContent());  // Prints 111 after
}

但该节点的文件内容保持不变。

如何设置该节点的值?

谢谢

I'm trying to set a node value via an XPath. I have the following but it doesn't seem to change the actual files value.

XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();

xPathExpression = "//test";
xPathValue= "111";

NodeList nodes = (NodeList) xPath.evaluate(xPathExpression, new InputSource(new FileReader(fileName)), XPathConstants.NODESET);

for (int k = 0; i < nodes.getLength(); i++)
{
    System.out.println(nodes.item(k).getTextContent());  // Prints original value
    nodes.item(k).setTextContent(xPathValue);
    System.out.println(nodes.item(k).getTextContent());  // Prints 111 after
}

But file contents for that node remain unchanged.

How do I set the value of that node?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你而言 2024-12-28 23:22:42

您只是更改内存中的值,而不是文件本身的值。您需要将修改后的文档写回到文件中:

Source source = new DOMSource(doc);
Result result = new StreamResult(new File(fileName));
Transformer xformer;
try {
    xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, result);
} catch (TransformerConfigurationException e) {
    // TODO Auto-generated catch block
} catch (TransformerFactoryConfigurationError e) {
    // TODO Auto-generated catch block
} catch (TransformerException e) {
    // TODO Auto-generated catch block
}

这些类都来自javax.xml.transform.*

(当然,您需要保存对文档的引用,以便可以写回它(即您将无法继续将其直接传递到evaluate))。

You're merely changing the value in memory, not in the file itself. You need to write the modified document back out to the file:

Source source = new DOMSource(doc);
Result result = new StreamResult(new File(fileName));
Transformer xformer;
try {
    xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, result);
} catch (TransformerConfigurationException e) {
    // TODO Auto-generated catch block
} catch (TransformerFactoryConfigurationError e) {
    // TODO Auto-generated catch block
} catch (TransformerException e) {
    // TODO Auto-generated catch block
}

These classes all come from javax.xml.transform.*.

(You'll need to save a reference to the document, of course, so that you can write back to it (i.e. you won't be able to continue passing it directly into evaluate)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文