gpt4 book ai didi

java - 用 xpath 替换对象列表中的值?

转载 作者:太空宇宙 更新时间:2023-11-04 07:55:11 26 4
gpt4 key购买 nike

xpath 是否能够替换 xml 文档的值?

以下内容获取所有名称元素,但我如何替换它们?

<accounts>
<account>
<name>Johndoe1<name>
<account>
<account>
<name>Johndoe2<name>
<account>
</account>

XPathExpression fax = xpath.compile("/accounts/account/name")
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}

最佳答案

Is xpath capable to replace values of a xml doc?

不,XPath 是 XML 文档的查询语言。因此,它仅从 XML 文档中提取节点和数据,但无法更改(添加、替换、删除节点)XML 文档。

当 XML 文档的处理导致创建另一个与原始文档不同的 XML 文档时,这称为 XML 转换。 XSLT 是一种专门为 XML 转换而设计的语言,是此类处理的最自然选择。

关于java - 用 xpath 替换对象列表中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13722018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com