-->

Xml Parser unexpected token error position:TEXT @1

2019-08-15 05:50发布

问题:

The problem is when I parse the XML from the original link for example (I am not posting the original link due to security purpose) http://example.com/ss.svc/APIabc?A=10&Key=XXXXX&From=19&To=221&Date=19-Apr-2016 then listview is not populated and the the logcat says error this

LogCat error-

 Unexpected token (position:TEXT @1:2 in java.io.StringReader@4255df00) 
 Shutting down VM
 threadid=1: thread exiting with uncaught exception (group=0x41b19438)
 FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.androidhive.CustomizedListView}: java.lang.NullPointerException
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
 at android.app.ActivityThread.access$700(ActivityThread.java:143)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:4960)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:511)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
 at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
 at com.example.androidhive.CustomizedListView.onCreate(CustomizedListView.java:48)
 at android.app.Activity.performCreate(Activity.java:5203)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
 ... 11 more

but when the same XML is put to another server link then with the link ending with .php for example this http://www.example.in/exapmle/sub.php then its showing the parsed data to listview.

Say my XML Format is like this

<APIabc
    xmlns="http://exapmle.org/">
    <APIabcd
        xmlns:a="http://ac.ssc.org/2014/21/abfaggkk"
        xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Route>
            <Time>2016-04-13 06:11:00 AM</Time>
            <Avail>xyz</Avail>
            <thumb_url>http://api.androidhive.info/music/images/adele.png</thumb_url>
            <BusLabel/>
        </Route>
    </APIabcd>
</APIabc>

CustomizedListView.java

public class CustomizedListView extends Activity {
        // All static variables
        static final String URL = "http://example.com/ss.svc/APIabc?A=10&Key=XXXXX&From=19&To=221&Date=19-Apr-2016";
        // XML node keys 
        static final String KEY_SONG = "Route"; // parent node
    static final String KEY_ID = "Avail";
    static final String KEY_TITLE = "Avail";
    static final String KEY_ARTIST = "Avail";
    static final String KEY_ARTIST2 = "Avail";
    static final String KEY_DURATION = "Avail";
    static final String KEY_THUMB_URL = "thumb_url";

        ListView list;
        LazyAdapter adapter;

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

            ArrayList&lt;HashMap&lt;String, String&gt;&gt; songsList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();

            XMLParser parser = new XMLParser();
            String xml = parser.getXmlFromUrl(URL); // getting XML from URL
            Document doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_SONG);
            // looping through all song nodes &lt;song&gt;
            for (int i = 0; i < nl.getLength(); i++) {
                // creating new HashMap
                HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key =&gt; value
                map.put(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

                // adding HashList to ArrayList
                songsList.add(map);
            }

            list=(ListView)findViewById(R.id.list);

            // Getting adapter by passing xml data ArrayList
            adapter=new LazyAdapter(this, songsList);
            list.setAdapter(adapter);

            // Click event for single list row
            list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                        int position, long id) {

                }
            });
        }
    }

As logcat says error com.example.androidhive.CustomizedListView.onCreate(CustomizedListView.java:48) so the line 48 in code

NodeList nl = doc.getElementsByTagName(KEY_SONG);

回答1:

Solved it by my self.The way i solved it is like- I commented out the below lines from the code

   // InputSource is = new InputSource();
   // is.setCharacterStream(new StringReader(xml));
    doc = db.parse(is); 

and passed the URL directly to this line

 doc = db.parse("http://yourURL.xml");