java-从资源加载和解析xml时出现问题

java-从资源加载和解析xml时出现问题,第1张

概述我已经编写了一个解析器,用于从HttpURLConnection解析XML文件.这很好.问题:我需要重写此文件,以便从本地资源而不是从Internet加载xml文件,但是我无法使它正常工作…只是让您了解原始Web解析器的外观:InputStreamin=null;URLConnectionconnection=url.openConnection();Ht

我已经编写了一个解析器,用于从httpURLConnection解析XML文件.这很好.

问题:我需要重写此文件,以便从本地资源而不是从Internet加载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 SAXParseExceptionElement 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:预期名称(java.io.inputStreamReader@47668728中的位置START_TAG @ 1:309)

非常感谢您的帮助:)

解决方法:

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

总结

以上是内存溢出为你收集整理的java-从资源加载和解析xml时出现问题全部内容,希望文章能够帮你解决java-从资源加载和解析xml时出现问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1093835.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存