Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use XPath to find the node value with CDATA tag in java

Tags:

java

xml

xpath

I used XPath to parse rss xml data, and the data is

<rss version="2.0">
  <channel>
    <title>
      <![CDATA[sports news]]>
    </title>
  </channel>
</rss>  

I want to get the text "sports news" using xpath "/rss/channel/title/text()" ,but the result is not what I want ,the real result is "\r\n",so how to found the result I want.

the code is below:

    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xPath = xpathFactory.newXPath();
    Node node = (Node) xPath.evaluate("/rss/channel/title/text()", doc,XPathConstants.NODE);
    String title = node.getNodeValue();
like image 931
user1010434 Avatar asked Nov 08 '11 03:11

user1010434


1 Answers

Try calling setCoalescing(true) on your DocumentBuilderFactory and this will collapse all CDATA/text nodes into single nodes.

like image 137
prunge Avatar answered Oct 18 '22 17:10

prunge