0
我有一个xml文件,我用XPATH解析它。但我同时获得内容出来的Xpath没有给出正确的结果
这里有问题是XML
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<reg>
<user>
<Name>abc def</Name>
<Email>ahjkhjkghjkhjk</Email>
<Picture>/mnt/sdcard/download/1357670177a386a-big-1.jpg</Picture>
<LastEdited>Mar 12, 2014 10:32:09 AM</LastEdited>
</user>
<user>
<Name>xy zabc</Name>
<Email>asdasdasdasd</Email>
<Picture>/mnt/sdcard/download/1357670177a386a-big-1.jpg</Picture>
<LastEdited>Mar 12, 2014 10:32:09 AM</LastEdited>
</user>
</reg>
,这里是我的代码用于解析它
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/reg/user/Name";
System.out.println(expression);
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
Users_List.add(nodeList.item(i).getFirstChild().getNodeValue());
}
这种表达"reg/user"
它返回任何结果和"reg/user/Name"
或"reg/user/Email"
它会返回正确的结果。我已经用在线测试器测试了表达式,它给出了正确的结果。有我的解析代码蚂蚁问题..?
还是同样的结果。 。 。 –
使用getTextContent()代替getNodeValue() – Fireworks
@Fireworks:感谢它的工作:) –