1

我正在尝试使用 Android 从 URL 解析 XML。当我运行我的应用程序时,我收到以下错误:

06-04 10:57:50.250: W/System.err(16811): org.apache.harmony.xml.ExpatParser$ParseException: At line 29, column 854: mismatched tag
06-04 10:57:50.250: W/System.err(16811):    at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:519)
06-04 10:57:50.250: W/System.err(16811):    at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:478)
06-04 10:57:50.250: W/System.err(16811):    at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:317)
06-04 10:57:50.250: W/System.err(16811):    at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
06-04 10:57:50.250: W/System.err(16811):    at com.first.MyApp.MyApp$downloadSaAsnyc.doInBackground(MyApp.java:970)
06-04 10:57:50.250: W/System.err(16811):    at com.first.MyApp.MyApp$downloadSaAsnyc.doInBackground(MyApp.java:1)
06-04 10:57:50.250: W/System.err(16811):    at android.os.AsyncTask$2.call(AsyncTask.java:252)
06-04 10:57:50.250: W/System.err(16811):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-04 10:57:50.250: W/System.err(16811):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-04 10:57:50.250: W/System.err(16811):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
06-04 10:57:50.250: W/System.err(16811):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
06-04 10:57:50.250: W/System.err(16811):    at java.lang.Thread.run(Thread.java:1020)

我试图解析的 URL 中的 XML 看起来像这样......

<ScheduledAudienceResponse>
    <ScheduledAudiences>
        <ScheduledAudience>
            <audience_id>1</audience_id>
            <audience_name>Jones Kapate</audience_name>
            <firstname>Jones</firstname>
            <lastname>Kapate</lastname>
            <id_number>123434</id_number>
            <branch_id>164</branch_id>
            <audience_group_id>8</audience_group_id>
            <scheduled_date>2012-06-04</scheduled_date>
        </ScheduledAudience>
        <ScheduledAudience>
            <audience_id>4</audience_id>
            <audience_name>John Doe</audience_name>
            <firstname>John</firstname>
            <lastname>Doe</lastname>
            <id_number>12312</id_number>
            <branch_id>164</branch_id>
            <audience_group_id>8</audience_group_id>
            <scheduled_date>2012-06-04</scheduled_date>
        </ScheduledAudience>
    </ScheduledAudiences>
</ScheduledAudienceResponse>

我用来解析上述 XML 代码的代码如下所示......

String sa_target_url = "http://my.app.com/scheduled_audiences/scheduled_audience_xml/" 
                        + username + "/" + password + "/" + current_date + "/"
                        + device_id;

Log.v( LOG_TAG, "sa_target_url: " + sa_target_url );

URL sa_url = new URL( sa_target_url );

sa_url.openConnection();

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();

ScheduledAudienceHandler myScheduledAudienceHandler = new ScheduledAudienceHandler();

xr.setContentHandler( myScheduledAudienceHandler );
xr.parse( new InputSource( sa_url.openStream() ) );

谢谢你的帮助!

4

0 回答 0