我用XPath解析了rss xml数据,数据是
<rss version="2.0">
<channel>
<title>
<![CDATA[sports news]]>
</title>
</channel>
</rss>
Run Code Online (Sandbox Code Playgroud)
我想使用 xpath "/rss/channel/title/text()" 获取文本 "sports news" ,但结果不是我想要的,真正的结果是 "\r\n",那么如何找到结果我想要。
代码如下:
文档文档 = 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();
尝试在 DocumentBuilderFactory 上调用setCoalescing(true) ,这会将所有 CDATA/文本节点折叠为单个节点。
| 归档时间: |
|
| 查看次数: |
5880 次 |
| 最近记录: |