从资源加载和解析xml的问题

我编写了一个解析器,它解析来自HttpURLConnection的xml文件。 这很好用。

问题:我需要重写这个,以便从本地资源而不是从互联网加载xml文件,但我无法让它工作……只是为了让你知道原始网络解析器的外观:

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...... 

现在继续使用我用于尝试从位于resources文件夹中的xml / myfile.xml的本地资源进行解析的代码:

 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文件和web文件完全相同……如果有人想看看它: http : //agens.no/Eniro/Android/SEWheel/Category2.plist.xml

并且inheritance了堆栈跟踪: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)

准确的所有你的帮助:)

找到答案..当文件是我的res / xml文件夹时,输入流显示了很多无效字符。 当我把它放在res / raw文件夹中时它工作正常。