SAX解析器引发一些错误

时间:2012-02-08 16:05:24

标签: java android

当我基于PHP的Web服务时,我遇到了一些解析问题。我从2天开始陷入困境。我无法在Activity中获得任何输出。当我在Handler中打印它时,我成功打印变量 这里有一些logcate:

02-08 00:51:04.889: WARN/System.err(606): org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 805: not well-formed (invalid token)
02-08 00:51:04.893: WARN/System.err(606):     at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:523)
02-08 00:51:04.893: WARN/System.err(606):     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:482)
02-08 00:51:04.893: WARN/System.err(606):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:320)
02-08 00:51:04.893: WARN/System.err(606):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:277)
02-08 00:51:04.893: WARN/System.err(606):     at com.ESM.viewAllpartActivity.onCreate(viewAllpartActivity.java:69)
02-08 00:51:04.893: WARN/System.err(606):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-08 00:51:04.904: WARN/System.err(606):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-08 00:51:04.904: WARN/System.err(606):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-08 00:51:04.904: WARN/System.err(606):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-08 00:51:04.904: WARN/System.err(606):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-08 00:51:04.904: WARN/System.err(606):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 00:51:04.914: WARN/System.err(606):     at android.os.Looper.loop(Looper.java:123)
02-08 00:51:04.914: WARN/System.err(606):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-08 00:51:04.914: WARN/System.err(606):     at java.lang.reflect.Method.invokeNative(Native Method)
02-08 00:51:04.914: WARN/System.err(606):     at java.lang.reflect.Method.invoke(Method.java:521)
02-08 00:51:04.914: WARN/System.err(606):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-08 00:51:04.914: WARN/System.err(606):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-08 00:51:04.914: WARN/System.err(606):     at dalvik.system.NativeStart.main(Native Method)

当我调用xml解析时。

这里我有过去的代码:: -

package com.awe;

import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class viewAllpartActivity extends Activity implements
        OnItemClickListener {
    /** Called when the activity is first created. */
    /* declare widgets */

    String nickname;
    String type;
    boolean flag = true;
    ListView lview3;

    blog_list_adapter adapter;

    String[] str_blogImage;
    String[] str_blogtitle;
    String[] str_morelink;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_allparty);

        Button home_logout;
        final Intent homeintent;
        homeintent = new Intent(viewAllpartActivity.this, MainActivity.class);
        home_logout = (Button) findViewById(R.id.vap_logout);
        home_logout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(homeintent);
                finish();

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

            /** Send URL to parse XML Tags */
            URL sourceUrl = new URL(
                    "URL?api=ViewBooking_Admin");

            /** Create handler to handle XML Tags ( extends DefaultHandler ) */
            viewAllPartyHandler myXMLHandler = new viewAllPartyHandler();
            xr.setContentHandler(myXMLHandler);
            xr.parse(new InputSource(sourceUrl.openStream()));

            viewAllPartydataset parsedLoginDataSet = myXMLHandler
                    .getParsedRegistrationData();
            ArrayList<String> Bookingid = myXMLHandler.getbooking_id();
            ArrayList<String> partyname = myXMLHandler.br3;
            System.out.println("partyname :::: : "+partyname);
            new xyz().execute();

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

    }

    private class xyz extends AsyncTask<Void, Void, Void> {
        private final ProgressDialog dialog = new ProgressDialog(
                viewAllpartActivity.this);

        @Override
        protected void onPreExecute() {
            this.dialog.setMessage("Please Wait...");
            this.dialog.show();
            this.dialog.setIndeterminate(true);

            // put your code which preload with processDialog
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // put your code here
            try {
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();

                /** Send URL to parse XML Tags */
                URL sourceUrl = new URL(
                        "url?api=ViewBooking_Admin");

                /** Create handler to handle XML Tags ( extends DefaultHandler ) */
                viewAllPartyHandler myXMLHandler = new viewAllPartyHandler();
                xr.setContentHandler(myXMLHandler);
                xr.parse(new InputSource(sourceUrl.openStream()));

                viewAllPartydataset parsedLoginDataSet = myXMLHandler
                        .getParsedRegistrationData();
                ArrayList<String> Bookingid = myXMLHandler.getbooking_id();
                ArrayList<String> partyname = myXMLHandler.br3;
                System.out.println("partyname :::: : "+partyname);
                /*str_blogImage = Bookingid.toArray(new String[Bookingid.size()]);
                str_morelink = partyname.toArray(new String[partyname.size()]);*/
                System.out.println("partyname :::: : "+partyname);

            } catch (Exception e) {
                // TODO: handle exception
            }
            return null;
        }

        @Override
        protected void onPostExecute(final Void unused) {

            lview3 = (ListView) findViewById(R.id.vap_listview);
            /*lview3.setAdapter(new
                    ArrayAdapter<String>(viewAllpartActivity.this,android.R.layout.simple_list_item_1 ,
                            str_morelink));*/

            lview3.setOnItemClickListener(viewAllpartActivity.this);

            if (this.dialog.isShowing()) {
                this.dialog.dismiss();

            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        System.out.println("str_morelink ::: " + arg2);

        Bundle b = new Bundle();
        b.putString("url", str_morelink[arg2]);
        Intent i1 = new Intent(getBaseContext(), blogwebview.class);
        i1.putExtras(b);
        startActivity(i1);
        // finish();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent homeback;

            homeback = new Intent(viewAllpartActivity.this, home.class);
            startActivity(homeback);

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

2 个答案:

答案 0 :(得分:0)

查看您的示例here,结果不是很好。它包含未编码的'&amp;' chararcters。

正确的解决方案是更改XML生成过程,以便获得格式良好的XML文件。

但你可以通过替换所有“&amp;”来尝试解决方法在解析它之前,在整个文件中使用“&amp;”。

答案 1 :(得分:0)

您的xml存在问题,特别是'&amp;'字符,所以你要定义一个CDTA部分来保存值...