gpt4 book ai didi

java - 如何在java中使用xpathh读取xml的所有节点?

转载 作者:行者123 更新时间:2023-12-01 12:20:44 24 4
gpt4 key购买 nike

我有一个 xml 文件,我想读取 xml 所有子节点的值。我的 xml 是

 <branches>
<branch-area name="abc">
<branch>
<branch-name> xyz Street</branch-name>
<branchID>5689742</branchID>
<branchAddress>xyz address</branchAddress>
<atm>true</atm>
<branch>true</branch>
<tab title="Contact">
<![CDATA[<table>
<tr>
<td class="head">Branch</td>
<td>bandra Street</td>
</tr>
<tr>
<td class="head">Address</td>
<td>bandra Street</td>
</tr>
<tr>
<td class="head">Sort Code</td>
<td>215863</td>
</tr>
</table>]]>
</tab>
</branch>
</branch-area>
<branch-area name="def 11">
<branch>
<branch-name>pqr</branch-name>
<branchID>123456</branchID>
<branchAddress>pqr address </branchAddress>
<atm>true</atm>
<branch>true</branch>
<tab title="Contact">
<![CDATA[<table>
<tr>
<td class="head">Branch</td>
<td>bandra Street</td>
</tr>
<tr>
<td class="head">Address</td>
<td>bandra Street</td>
</tr>
<tr>
<td class="head">Sort Code</td>
<td>215863</td>
</tr>
</table>]]>
</tab>
</branch>
</branch-area>
<branch-area name="ghi 14">
<branch>
<branch-name>jkl</branch-name>
<branchID>589674</branchID>
<branchAddress>jkl address</branchAddress>
<atm>true</atm>
<branch>true</branch>
<tab title="Contact">
<![CDATA[<table>
<tr>
<td class="head">Branch</td>
<td>bandra Street</td>
</tr>
<tr>
<td class="head">Address</td>
<td>bandra Street</td>
</tr>
<tr>
<td class="head">Sort Code</td>
<td>215863</td>
</tr>
</table>]]>
</tab>
</branch>
</branch-area>
</branches>

我正在使用这个 xpath 表达式来获取特定的分支。

String  xpathExpression = "/branches/branch-area[name='abc']/branch";

它返回给我特定的分支,即

    <branch>
<branch-name> xyz Street</branch-name>
<branchID>5689742</branchID>
<branchAddress>xyz address</branchAddress>
<atm>true</atm>
<branch>true</branch>
<tab title="Contact">
<![CDATA[<table>
<tr>
<td class="head">Branch</td>
<td>bandra Street</td>
</tr>
<tr>
<td class="head">Address</td>
<td>bandra Street</td>
</tr>
<tr>
<td class="head">Sort Code</td>
<td>215863</td>
</tr>
</table>]]>
</tab>
</branch>

但我想获取其子节点的值,例如分支名称branchID等,为此我是否需要为每个子节点使用另一个xpath表达式,或者还有其他方法吗?如果是,请指导我

最佳答案

你可以...

使用像 /branches/branch-area[@name='abc']/branch/* 这样的路径,它将返回所有子节点的 NodeList包含在分支中。

XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression branchExp = xpath.compile("/branches/branch-area[@name='abc']/branch/*");
NodeList branchNodes = (NodeList) branchExp.evaluate(dom, XPathConstants.NODESET);
System.out.println(branchNodes.getLength());
for (int index = 0; index < branchNodes.getLength(); index++) {
Node node = branchNodes.item(index);
System.out.println(node.getTextContent());
}

输出类似...

5
xyz Street
5689742
xyz address
true

你可以...

使用 branch 节点作为搜索的父节点来搜索每个单独的子节点...

XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression branchExp = xpath.compile("/branches/branch-area[@name='abc']/branch");
Node branchNode = (Node) branchExp.evaluate(dom, XPathConstants.NODE);

XPathExpression nameExp = xpath.compile("branch-name/text()");
String name = (String) nameExp.evaluate(branchNode, XPathConstants.STRING);
System.out.println("Name = " + name);

输出类似...

Name =  xyz Street

已更新...

因此,CDATA 不会被 xPath 处理,而是需要获取 tab 节点的文本,并将其解析为 Document 并在其上运行 xPath,例如...

try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(new File("Test.xml"));
XPath xpath = XPathFactory.newInstance().newXPath();

// Find the "thing" node...
XPathExpression thingExpr = xpath.compile("/branches/branch-area/branch/tab");
Node things = (Node) thingExpr.evaluate(dom, XPathConstants.NODE);
String table = things.getTextContent();

ByteArrayInputStream bais = new ByteArrayInputStream(table.getBytes());
Document tblDom = db.parse(bais);
XPathExpression tableExp = xpath.compile("/table/tr[td[text()='Sort Code']]/td[not(@*)]");
NodeList nodes = (NodeList) tableExp.evaluate(tblDom, XPathConstants.NODESET);
System.out.println(nodes.getLength());
for (int index = 0; index < nodes.getLength(); index++) {
Node node = nodes.item(index);
System.out.println(node.getTextContent());
}
} catch (Exception exp) {
exp.printStackTrace();
}

关于java - 如何在java中使用xpathh读取xml的所有节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26668446/

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