如何在android中使用dom解析本地xml文件?
我看过很多关于使用 Sax 解析本地 xml 文件的示例,但我需要使用 DOM 来完成此操作。我的 xml 文件位于 res/xml 文件夹下。这是代码:
InputStream in = getResources().openRawResource(R.xml.maps);
StringBuffer str = new StringBuffer();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
DocumentBuilder db = dbf.newDocumentBuilder ();
Document document = db.parse(in, null);
document.getDocumentElement ().normalize ();
但它不起作用。我认为 InputStream 行缺少一些内容。这对我来说真的很重要,我需要你的帮助。
I have seen lots of examples about parsing local xml files with Sax but i need to do this with DOM. My xml file is under res/xml folder. Here is the code:
InputStream in = getResources().openRawResource(R.xml.maps);
StringBuffer str = new StringBuffer();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
DocumentBuilder db = dbf.newDocumentBuilder ();
Document document = db.parse(in, null);
document.getDocumentElement ().normalize ();
But it doesnt work. I think something missing with the line InputStream. It is really important for me, i need your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也有同样的。我想知道为什么这个 openRawResource 会损坏 XML 文件,那是因为它被放置在 r.xml.* 中而不是 r.raw.* 中:)
I had the same. I was wondering why this openRawResource gets the XML file corrupted, and that's because it was placed in r.xml.* not in r.raw.* :)
我已经解决了这个问题。只需要改变一些小事情:
我将“this”添加到该行,并且我还将maps.xml 文件放在res/raw 下。
I have solved the problem. Just need to change small thing:
I added "this" to that line and also I have put the maps.xml file under res/raw.