Android中从字符串解析xml的问题
我想从字符串中解析 XML。当字符串长度很小时,这工作得很好。但对于长字符串,它不会解析任何内容。它在调用 parse() 时暂停。我使用以下代码进行解析。请告诉我我的代码有什么问题?
//my xml code is string 'xmlString'
DocumentBuilderFactory objDocFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder objDocBuilder=objDocFactory.newDocumentBuilder();
InputSource objInput=new InputSource();
objInput.setCharacterStream(new StringReader(xmlString));
Document objDoc=objDocBuilder.parse(objInput);
//appication is suspended at the above line
objDoc.getDocumentElement().normalize();
谢谢...
I want to parse an XML from a string. This is working fine when the string length is small. But for a long string it is not parsing anything. It is suspended on calling parse(). I use the following code for parsing. Please tell me what is the wrong with my code?
//my xml code is string 'xmlString'
DocumentBuilderFactory objDocFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder objDocBuilder=objDocFactory.newDocumentBuilder();
InputSource objInput=new InputSource();
objInput.setCharacterStream(new StringReader(xmlString));
Document objDoc=objDocBuilder.parse(objInput);
//appication is suspended at the above line
objDoc.getDocumentElement().normalize();
Thank you...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文档有多大?
我已经使用了 Android 版的 TinyXML 解析器,并且对结果非常满意。也许你可以检查一次。
最好的部分是它非常易于使用,并且学习曲线要少得多。
How large is the document?
I have used TinyXML parser for Android, and am pretty satisfied with the result. Maybe you can check it out once.
Best part is it is pretty ease to use, and the learning curve is considerably less.
正如基思所说。使用 DOM,它会尝试将整个文档加载到内存中,这在移动设备上并不是最好的事情。 SAX 将流式传输数据,因此只使用少量内存。
Exactly what Keith said. Using DOM it's trying to load the entire document into memory, which is not the best thing on a mobile device. SAX will stream the data so it will only use a little memory.