在android中解析CDATA
我正在解析服务器上的 XML,我读取并解析它,没有任何错误,但我看不到数据。
这是我的 XML:
<BookData><book><Title><![CDATA[ABC]]></Title><AuthorFName1><![CDATA[A]]></AuthorFName1><AuthorLName1><![CDATA[B]]></AuthorLName1></book><book><Title><![CDATA[XYZ]]></Title><AuthorFName1><![CDATA[A]]></AuthorFName1><AuthorLName1><![CDATA[B]]></AuthorLName1></book>
我正在使用 DocumentBuilderFactory
查看代码,即使我设置了
dbf.setCoalescing(true);
但仍然不起作用,请查看 DocumentBuilderFactory 的代码
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setCoalescing(true);
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.d("XML parse Error:", e.getMessage());
return null;
} catch (SAXException e) {
Log.d("Wrong XML File Structure", e.getMessage());
return null;
} catch (IOException e) {
Log.d("IOException", e.getMessage());
return null;
}
I'm parsing XML which is on the server I read it and parse it There is no any error But I'm unable to see the data.
Here is my XML:
<BookData><book><Title><![CDATA[ABC]]></Title><AuthorFName1><![CDATA[A]]></AuthorFName1><AuthorLName1><![CDATA[B]]></AuthorLName1></book><book><Title><![CDATA[XYZ]]></Title><AuthorFName1><![CDATA[A]]></AuthorFName1><AuthorLName1><![CDATA[B]]></AuthorLName1></book>
I'm using DocumentBuilderFactory
see the code even I set
dbf.setCoalescing(true);
But still not working please see the code for DocumentBuilderFactory
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setCoalescing(true);
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.d("XML parse Error:", e.getMessage());
return null;
} catch (SAXException e) {
Log.d("Wrong XML File Structure", e.getMessage());
return null;
} catch (IOException e) {
Log.d("IOException", e.getMessage());
return null;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是示例 XML:
假设您已经将 xml 文件内容作为 xmlString,以下方法将解析带或不带 cdata 标记的 xml:
This is the sample XML:
Let's say you already have the xml file content as xmlString, the following methods will parse xml with or without cdata tag:
尝试一下,您只需将 InputSource 实例传递给此方法即可。
Try this, you just have to pass InputSource instance to this method and it works.