首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Java中使用XPath解析

如何在Java中使用XPath解析
EN

Stack Overflow用户
提问于 2018-06-15 06:23:29
回答 1查看 36关注 0票数 0

我希望得到一个TagName“控件”。我正在使用XPath来解析我的XML文件。目前我只得到一个空白窗口,我不知道为什么。

我的XML文件:

代码语言:javascript
复制
<tags>
  <row Id="1" TagName="soccer" Count="7" ExcerptPostId="12371" WikiPostId="12370" />
  <row Id="2" TagName="servos" Count="63" ExcerptPostId="186" WikiPostId="185" />
  <row Id="3" TagName="control" Count="394" ExcerptPostId="192" WikiPostId="191" />
  <row Id="5" TagName="gait" Count="4" ExcerptPostId="12362" WikiPostId="12361" />
</tags>

这是我的Main.java

代码语言:javascript
复制
String uri = "/home/files/Tags.xml";

try {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(uri);
    XPath xPath = XPathFactory.newInstance().newXPath();

    String output = (String) xPath.evaluate("/tags/row[@TagName='control']", doc.getDocumentElement(),
            XPathConstants.STRING);
    System.out.println("My output:" + output);
} catch (IOException | ParserConfigurationException | XPathExpressionException | SAXException e) { }

预期输出应为:

控制我的输出:

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50866749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档