gpt4 book ai didi

java - 在java中使用XPATH过滤XML文档

转载 作者:行者123 更新时间:2023-11-30 07:53:54 25 4
gpt4 key购买 nike

我有以下 XML 文档,需要使用 xpath 表达式进行过滤:

<List>
<Item>
<Name>abc</Name>
<Category><Name>123</Name><value>aaa</value></Category>
</Item>
<Item>
<Name>def</Name>
<Category><Name>456</Name><value>bbb</value></Category>
</Item>
<Item>
<Name>xyz</Name>
<Category><Name>123</Name><value>ccc</value></Category>
</Item>
</List>

我提供以下表达式:“/List/Item/Category[Name='123']”,这给了我:

<Category>
<Name>123</Name>
<value>aaa</value>
<Category>
<Name>123</Name>
<value>ccc</value>
</Category>
</Category>

但我需要这个输出:

<List>
<Item>
<Name>abc</Name>
<Category><Name>123</Name><value>aaa</value></Category>
</Item>
<Item>
<Name>xyz</Name>
<Category><Name>123</Name><value>ccc</value></Category>
</Item>
</List>

最佳答案

xPath 将返回当前文档中与您的条件相匹配的节点

如果您想生成 Document 的“过滤”表示,那么您需要创建第二个 Document 并附加匹配的 Node > 到它

例如...

try {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
Document original = b.parse(...);
original.getDocumentElement().normalize();

Document filtered = b.newDocument();
Node root = filtered.createElement("List");
filtered.appendChild(root);

String expression = "/List/Item/Category[Name='123']";
XPath xPath = XPathFactory.newInstance().newXPath();
Object result = xPath.compile(expression).evaluate(original, XPathConstants.NODESET);

NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {

Node node = nodes.item(i);
filtered.adoptNode(node);
root.appendChild(node);

}

try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {

Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty(OutputKeys.METHOD, "xml");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

DOMSource domSource = new DOMSource(filtered);
StreamResult sr = new StreamResult(os);
tf.transform(domSource, sr);

String text = new String(os.toByteArray());
System.out.println(text);

} catch (TransformerException ex) {
ex.printStackTrace();
}

} catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException | DOMException exp) {
exp.printStackTrace();
}

因此,根据您的原始 XML,这将输出

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<List>
<Category>
<Name>123</Name>
<value>aaa</value>
</Category>
<Category>
<Name>123</Name>
<value>ccc</value>
</Category>
</List>

请注意,当节点附加到新文档时,这将从原始文档中删除节点。

i want to just filter the result without losing the xml structure

XPath 很像 SQL,它是一种查询语言,它的工作不是生成新的结构。话虽如此,您可以控制它返回哪个节点,例如,如果您将查询更改为更像这样的内容...

String expression = "/List/Item[Category[Name='123']]";

然后输出变得更像......

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<List>
<Item>
<Name>abc</Name>
<Category>
<Name>123</Name>
<value>aaa</value>
</Category>
</Item>
<Item>
<Name>xyz</Name>
<Category>
<Name>123</Name>
<value>ccc</value>
</Category>
</Item>
</List>

关于java - 在java中使用XPATH过滤XML文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32959791/

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