How to use XPath to search for node value with CDATA tag in java

I used XPath to parse rss xml data and data

<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 find the result that I want.

code 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 ();
+4
source share
2 answers

Try calling setCoalescing (true) on your DocumentBuilderFactory, and this will break all CDATA / text nodes into separate nodes.

+3
source

You can try changing the XPath expression to

 "string(/rss/channel/title)" 

and use the return type STRING instead of NODE:

 Node node = (Node) xPath.evaluate("string(/rss/channel/title)", doc, XPathConstants.STRING); 

Thus, you do not select the text node, but rather the string value of the title element, consisting of the concatenation of all its streaming text nodes.

0
source

Source: https://habr.com/ru/post/1380119/


All Articles