Android Getting Error While Parseing The XML - At

2019-05-19 02:11发布

Problem is The Error above mentioned

my xml which i am getting from web

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

my code to parse 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()));

//ON GOOGLE I FOUND THIS SOLUTION BUT THIS GIVE ME ERROR FOR NOT WELL FORMED TOKEN AT LINE 15

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

and log is

    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)

Please help me in this. If you ever had the same problem, what have you tried to resolve...please share ur tricks with me..

Thank You, Mayur Parekh

1条回答
The star\"
2楼-- · 2019-05-19 02:33

Your reading your inputstream twice.

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

This:

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

and

  xmlReader.parse(is);

both read the InputStream, that is a no no.

Remove the BufferedReader and the while loop (they were only there to confirm the XML had downloaded anyway which you say it has)

I can't find a link to back up my theory. Best I could find -Reading an IS twice-

查看更多
登录 后发表回答