你需要遵循以下要求:
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>);
然后,调用
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。



