如何在 Java 中使用 XPath 读取 XML [英] How to read XML using XPath in Java

查看:23
本文介绍了如何在 Java 中使用 XPath 读取 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Java 中使用 XPath 读取 XML 数据,因此对于我收集的信息,我无法根据我的要求解析 XML.

这是我想要做的:

通过URL从网上获取XML文件,然后使用XPath解析它,我想在其中创建两个方法.一个是我输入一个特定的节点属性id,然后我得到所有的子节点作为结果,第二个是假设我只想得到一个特定的子节点值

<方法><主题名称="Java"><url>http://www.rgagnonjavahowto.htm</url><汽车>出租车</汽车></主题><主题名称="PowerBuilder"><url>http://www.rgagnon/pbhowto.htm</url><url>http://www.rgagnon/pbhowtonew.htm</url></主题><主题名称="Javascript"><url>http://www.rgagnon/jshowto.htm</url></主题><主题名称="VBScript"><url>http://www.rgagnon/vbshowto.htm</url></主题></如何>

在上面的例子中,如果我通过@name 搜索,我想读取所有元素,还有一个函数,我只希望来自 @name 'Javascript' 的 url 只返回一个节点元素.

解决方案

你需要一些类似的东西:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();文档 doc = builder.parse();XPathFactory xPathfactory = XPathFactory.newInstance();XPath xpath = xPathfactory.newXPath();XPathExpression expr = xpath.compile();

然后您调用 expr.evaluate() 传入在该代码中定义的文档和您期望的返回类型,并将结果转换为结果的对象类型.

如果您需要有关特定 XPath 表达式的帮助,您可能应该将其作为单独的问题提出(除非这是您在此处首先提出的问题 - 我理解您的问题是如何在 Java 中使用 API).

(对评论的回应):此 XPath 表达式将为您提供 PowerBuilder 下第一个 URL 元素的文本:

/howto/topic[@name='PowerBuilder']/url/text()

这会给你第二个:

/howto/topic[@name='PowerBuilder']/url[2]/text()

您可以通过以下代码获得:

expr.evaluate(doc, XPathConstants.STRING);

如果您不知道给定节点中有多少个 URL,那么您应该这样做:

XPathExpression expr = xpath.compile("/howto/topic[@name='PowerBuilder']/url");NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

然后遍历 NodeList.

I want to read XML data using XPath in Java, so for the information I have gathered I am not able to parse XML according to my requirement.

here is what I want to do:

Get XML file from online via its URL, then use XPath to parse it, I want to create two methods in it. One is in which I enter a specific node attribute id, and I get all the child nodes as result, and second is suppose I just want to get a specific child node value only

<?xml version="1.0"?>
<howto>
  <topic name="Java">
      <url>http://www.rgagnonjavahowto.htm</url>
  <car>taxi</car>
  </topic>
  <topic name="PowerBuilder">
       <url>http://www.rgagnon/pbhowto.htm</url>
       <url>http://www.rgagnon/pbhowtonew.htm</url>
  </topic>
  <topic name="Javascript">
        <url>http://www.rgagnon/jshowto.htm</url>
  </topic>
 <topic name="VBScript">
       <url>http://www.rgagnon/vbshowto.htm</url>
 </topic>
 </howto>

In above example I want to read all the elements if I search via @name and also one function in which I just want the url from @name 'Javascript' only return one node element.

解决方案

You need something along the lines of this:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(<uri_as_string>);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(<xpath_expression>);

Then you call expr.evaluate() passing in the document defined in that code and the return type you are expecting, and cast the result to the object type of the result.

If you need help with a specific XPath expressions, you should probably ask it as separate questions (unless that was your question in the first place here - I understood your question to be how to use the API in Java).

Edit: (Response to comment): This XPath expression will get you the text of the first URL element under PowerBuilder:

/howto/topic[@name='PowerBuilder']/url/text()

This will get you the second:

/howto/topic[@name='PowerBuilder']/url[2]/text()

You get that with this code:

expr.evaluate(doc, XPathConstants.STRING);

If you don't know how many URLs are in a given node, then you should rather do something like this:

XPathExpression expr = xpath.compile("/howto/topic[@name='PowerBuilder']/url");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

And then loop over the NodeList.

这篇关于如何在 Java 中使用 XPath 读取 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆