Android在解析XML时出错 - 在第1行,第0列 - 未找到任何元素

时间:2011-09-01 07:09:57

标签: android xml parsing saxparser

问题是上面提到的错误

我从网上获取的xml

           <?xml version="1.0" encoding="ISO-8859-1" ?>
       <Doc>
       <Root>
       <Record>
               <sar_id>1</sar_id>
               <sar_region><![CDATA[Antigua & Barbuda]]></sar_region>
               <sar_auth>Antigua and Barbuda Defence Force Coast Guards
       </sar_auth>
               <email>abdfcg@candw.ag</email>
               <primary><![CDATA[1 268 462 06 71]]></primary>
               <secondary></secondary>
       </Record>
       <Record>
               <sar_id>2</sar_id>
               <sar_region><![CDATA[Argentina]]></sar_region>
               <sar_auth>Comando de Operaciones Navals, Argentine Navy
       </sar_auth>
               <email>coopacsm@ara.mil.ar</email>
               <primary><![CDATA[54 2932 487 640?]]></primary>
               <secondary></secondary>
       </Record>
       <Record>
               <sar_id>3</sar_id>
               <sar_region><![CDATA[Aruba]]></sar_region>
               <sar_auth>SARFA
       </sar_auth>
               <email>sarfa16@hotmail.com</email>
               <primary><![CDATA[297 5 837371]]></primary>
               <secondary></secondary>
       </Record>
  </Root>
  </Doc>

我的代码来解析xml

        try{
    URL url = new URL(
            "http://myurl.com");
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxparser = factory.newSAXParser();

    XMLReader xmlReader = saxparser.getXMLReader();
    SARParser getTParser = new SARParser(itemList);
    xmlReader.setContentHandler(getTParser);


    InputSource is = new InputSource(url.openStream());
    is.setEncoding("ISO-8859-1");

    BufferedReader br = new BufferedReader(new InputStreamReader(is.getByteStream()));
    String line;
    while((line=br.readLine())!=null)
    {
        Log.i("Line", line);
    }
    xmlReader.parse(is);

               //xmlReader.parse(new InputSource(url.openStream()));

//在GOOGLE上我发现了这个解决方案但是这给了我第15行没有形成令牌的错误

} catch (Exception e) {
    e.printStackTrace();
}

并且日志是

    09-01 12:32:11.461: WARN/System.err(870): org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: no element found
09-01 12:32:11.492: WARN/System.err(870):     at org.apache.harmony.xml.ExpatParser.finish(ExpatParser.java:538)
09-01 12:32:11.492: WARN/System.err(870):     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:468)
09-01 12:32:11.492: WARN/System.err(870):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329)
09-01 12:32:11.492: WARN/System.err(870):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286)
09-01 12:32:11.492: WARN/System.err(870):     at com.openxcell.sar.Profile.onCreate(Profile.java:77)
09-01 12:32:11.492: WARN/System.err(870):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
09-01 12:32:11.492: WARN/System.err(870):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
09-01 12:32:11.492: WARN/System.err(870):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
09-01 12:32:11.492: WARN/System.err(870):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
09-01 12:32:11.492: WARN/System.err(870):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
09-01 12:32:11.492: WARN/System.err(870):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 12:32:11.521: WARN/System.err(870):     at android.os.Looper.loop(Looper.java:123)
09-01 12:32:11.521: WARN/System.err(870):     at android.app.ActivityThread.main(ActivityThread.java:4203)
09-01 12:32:11.521: WARN/System.err(870):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 12:32:11.521: WARN/System.err(870):     at java.lang.reflect.Method.invoke(Method.java:521)
09-01 12:32:11.521: WARN/System.err(870):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-01 12:32:11.521: WARN/System.err(870):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
09-01 12:32:11.521: WARN/System.err(870):     at dalvik.system.NativeStart.main(Native Method)

请帮帮我。 如果你有同样的问题,你试图解决什么......请与我分享你的技巧..

谢谢你, Mayur Parekh

1 个答案:

答案 0 :(得分:7)

您两次阅读输入流。

 InputSource is = new InputSource(url.openStream());

此:

 new BufferedReader(new InputStreamReader(is.getByteStream()));

  xmlReader.parse(is);

都读取了InputStream,这是一个no no。

删除BufferedReader和while循环(它们只用于确认XML已经下载你说它已经

我找不到支持我的理论的链接。 Best I could find -Reading an IS twice-