如何在Xpath中获取节点值 - Java

bla*_*hli 5 java xml rss xpath parsing

我有一段XML看起来像这样:

<entry>
<id>tag:example.com,2005:Release/343597</id>
<published>2012-04-10T11:29:19Z</published>
<updated>2012-04-10T12:04:41Z</updated>
<link type="text/html" href="http://example.com/projects/example1" rel="alternate"/>
<title>example1</title>
</entry>
Run Code Online (Sandbox Code Playgroud)

我需要抓住http://example.com/projects/example1这个块的链接.我不知道该怎么做.要获得项目的标题,我使用此代码:

String title1 = children.item(9).getFirstChild().getNodeValue();
Run Code Online (Sandbox Code Playgroud)

childrengetChildNodes()对象在哪里<entry> </entry>.但是NullPointerExceptions当我尝试以<link>类似的方式获取节点的节点值时,我不断得到.我看到<link>节点的XML代码不同,我不确定它的价值是什么......请指教!

das*_*h1e 14

获取该节点的xpath表达式是

//entry/link/@href
Run Code Online (Sandbox Code Playgroud)

在java中你可以写

Document doc = ... // your XML document
XPathExpression xp = XPathFactory.newInstance().newXPath().compile("//entry/link/@href");
String href = xp.evaluate(doc);
Run Code Online (Sandbox Code Playgroud)

然后,如果需要获取link具有特定条目的条目的值,则id可以将xpath表达式更改为

//entry[id='tag:example.com,2005:Release/343597']/link/@href
Run Code Online (Sandbox Code Playgroud)

最后,如果要获取文档中的所有链接,如果文档中有许多条目元素,则可以编写

Document doc = ... // your XML document
XPathExpression xp = XPathFactory.newInstance().newXPath().compile("//entry/link/@href");
NodeList links = (NodeList) xp.evaluate(doc, XPathConstants.NODESET);
// and iterate on links
Run Code Online (Sandbox Code Playgroud)


Pha*_*ani 6

这是完整的代码:

    DocumentBuilderFactory domFactory = DocumentBuilderFactory
            .newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("test.xml");
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("//entry/link/@href");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
         System.out.println(nodes.item(i));
    }
Run Code Online (Sandbox Code Playgroud)