从资源的问题加载和解析XML [英] problem loading and parsing xml from resources
问题描述
I'm写了一个解析器解析至极从一个XML文件从HttpURLConnection的。这工作得很好。
I´m have written a parser wich parses a xml file from a from a HttpURLConnection. This works fine.
问题:我需要重写本,这样XML文件是从当地的资源,而不是从互联网上下载,但我不能得到这个工作......只给哟一个想法如何原来的网络分析器的样子:
Problem: I need to rewrite this so that the xml file is loaded from local resources instead of from the internet, but I cant get this to work... Just to give yo an idea how the original web parser looks:
InputStream in=null;
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
//START PARSING......
现在继承人的code我使用的尝试从放置在XML本地资源解析/ myfile.xml中的资源文件夹中:
Now heres the code I am using to try to parse from a local resource placed at xml/myfile.xml in the resources folder:
InputStream in=null;
in = mParentActivity.getResources().openRawResource(R.xml.myfile);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(in); // THIS IS WHERE I GET AN SAXParseException
Element root = dom.getDocumentElement();
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
//START PARSING......
本地XML文件和网页文件是完全一样的,如果还有人会来看看它:的 http://agens.no/Eniro/Android/SEWheel/Category2.plist.xml
和继承人的堆栈跟踪: 02-01 16:08:45.546:WARN / System.err的(19703):org.xml.sax.SAXParseException:名字预期(位置:START_TAG @ 1:309 java.io.InputStreamReader@47668728)
And heres the stacktrace: 02-01 16:08:45.546: WARN/System.err(19703): org.xml.sax.SAXParseException: name expected (position:START_TAG @1:309 in java.io.InputStreamReader@47668728)
preciate所有侑帮助:)
Preciate all yor help :)
推荐答案
找到了答案。当该文件是我的RES / XML文件夹,InputStream的表现出了极大的无效字符。当我把它在res /原始文件夹,它工作得很好。
Found the answer.. When the file was i the res/xml folder, the inputstream showed a lot of invalid characters. When i put it in the res/raw folder it worked fine.
这篇关于从资源的问题加载和解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!