作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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/
我是一名优秀的程序员,十分优秀!