我已经编写了一个解析器,它可以从 HttpURLConnection 解析 xml 文件。这很好用。
问题:我需要重写这个,以便从本地资源而不是从互联网加载 xml 文件,但我无法让它工作...只是为了让您了解原始 Web 解析器的外观:
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......
现在,这是我用来尝试从资源文件夹中的 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
这是堆栈跟踪:
02-01 16:08:45.546:WARN / System.err(19703):org.xml.sax.SAXParseException:预期名称(位置:START_TAG @1:309 in java.io.InputStreamReader@47668728)
找到答案。当文件位于 res/xml 文件夹中时,输入流显示许多无效字符。当我把它放在 res/raw 文件夹中时,它工作正常。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)