Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从资源加载和解析xml时出现问题_Java_Android_Xml_Resources_Local - Fatal编程技术网

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

Java 从资源加载和解析xml时出现问题,java,android,xml,resources,local,Java,Android,Xml,Resources,Local,我已经编写了一个解析器,它从HttpURLConnection解析来自的xml文件。这个很好用 问题:我需要重写它,以便从本地资源而不是从internet加载xml文件,但我无法让它工作。。。让您了解一下原始web解析器的外观: InputStream in=null; URLConnection connection = url.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection)connectio

我已经编写了一个解析器,它从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......
InputStream in=null;
URLConnection=url.openConnection();
HttpURLConnection httpConnection=(HttpURLConnection)连接;
int responseCode=httpConnection.getResponseCode();
如果(responseCode==HttpURLConnection.HTTP_OK){
in=httpConnection.getInputStream();
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
文档dom=db.parse(in);
Element docEle=dom.getDocumentElement();
NodeList nl=docEle.getChildNodes();
对于(int i=0;i
下面是我尝试从位于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......
InputStream in=null;
in=mParentActivity.getResources().openRawResource(R.xml.myfile);
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
Document dom=builder.parse(in);//这是我得到SAXParseException的地方
Element root=dom.getDocumentElement();
NodeList nl=root.getChildNodes();
对于(int i=0;i
本地xml文件和web文件完全相同……如果有人看一下的话:

这是stacktrace:

02-01 16:08:45.546:WARN/System.err(19703):org.xml.sax.SAXParseException:应为名称(位置:java.io中的START_TAG@1:309)。InputStreamReader@47668728)


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

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

这表明ts XML无效。您是否尝试过将资源读入字符串并打印出来,以查看它读取的内容?您的代码看起来很好,您确定这两个XML文件是相同的吗?您是否尝试过区分它们?听起来好像有些地方您有一些东西,看起来像是的,我已将其读入字符串,看起来很好..看起来很正确如图所示:我也没有任何类似于此的标记:S感谢您的帮助,这表明XML无效。您是否尝试过将资源读入字符串并打印出来,以查看它读取的内容?您的代码看起来很好,您确定这两个XML文件是相同的吗?您是否尝试过对它们进行区分?听起来很简单例如,如果你有一些看起来像是的,我已经把它读成了一个字符串,它看起来很好。它看起来像这里一样正确:我也没有任何看起来像这样的标签