Android中从字符串解析xml的问题

发布于 2024-10-05 23:33:20 字数 619 浏览 0 评论 0原文

我想从字符串中解析 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笔一画续写前缘 2024-10-12 23:33:20

文档有多大?

我已经使用了 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.

深居我梦 2024-10-12 23:33:20

正如基思所说。使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文