我有一个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 expreesion获取特定分支。
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>
但我希望得到它的子项的值,如branch-name branchID等,我是否需要为每个子节点使用另一个xpath表达式,或者还有另一种方法?如果是,请指导我
答案 0 :(得分:1)
使用类似/branches/branch-area[@name='abc']/branch/*
的路径,它会返回NodeList
中包含的所有子节点的branch
。
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();
}