0

I am attempting to parse XML data from the following URL

Traffic Scotland Current Incidents

Right now, I have an app that, on button press, gets the XML data, attempts to parse it, and then just displays the raw xml data as I can't currently parse anything.

As far as I can tell, the code below should work, and I'm at a loss for why it doesn't. It seems to go straight from START_DOCUMENT (eventType 0) to END_DOCUMENT (eventType 1).

public void onClick(View view)
{
    if (view == ciButton)
        viewCI();

    if (view == prButton)
    {
        viewPR();
    }
}

public void viewCI()
{
    new Thread(new Task(urlCI)).start();
}

public void viewPR()
{
    new Thread(new Task(urlPR)).start();
}

class Task implements Runnable
{
    private String url1;
    private String text;

    Incident incident = null;
    ArrayList<Incident> incidents = null;

    public Task (String aUrl)
    {
        url1 = aUrl;
    }

    @Override
    public void run()
    {
        String inputLine;
        Log.e("MyTag","in run");

        try
        {
            Log.e("MyTag","in try");

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            URL url = new URL(url1);
            URLConnection uc = url.openConnection();
            InputStreamReader isr = new InputStreamReader(uc.getInputStream());
            BufferedReader in = new BufferedReader(isr);

            int lineNum = 0;
            //Skips first line of XML feed
            while ((inputLine = in.readLine()) != null)
            {
                lineNum++;
                if (lineNum > 1)
                {
                    result = result + inputLine;
                }
            }

            xpp.setInput(uc.getInputStream(),null);

            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT)
            {
                Log.e("MyTag","in while");
                Log.e("MyTag",Integer.toString(eventType));
                if (eventType == XmlPullParser.START_DOCUMENT)
                {
                    Log.e("MyTag","Start Doc");
                }
                else if (eventType == XmlPullParser.START_TAG)
                {
                    String tag = xpp.getName();
                    Log.e("MyTag","Start tag: "+tag);
                }
                else if (eventType == XmlPullParser.END_TAG)
                {
                    String tag = xpp.getText();
                    Log.e("MyTag","End Tag: "+tag);
                }
                else if (eventType == XmlPullParser.TEXT)
                {
                    String tag = xpp.getText();
                    Log.e("MyTag","Text: "+tag);
                }


                Log.e("MyTag","xpp.next");
                eventType = xpp.next();
                Log.e("MyTag","ET after next: "+Integer.toString(eventType));
            }

        }
        catch (XmlPullParserException xppe)
        {
            Log.e("MyTag","XPPE: ");
            xppe.printStackTrace();
        }
        catch (IOException ioe)
        {
            Log.e("MyTag", "IOE");
            ioe.printStackTrace();
        }

        MainActivity.this.runOnUiThread(new Runnable()
        {
            @Override
            public void run() {
                Log.d("UI thread", "I am the UI thread");
                urlInput.setText(result);
            }
        });
    }
}

When I run the code above, I log the following:

02-21 09:17:52.201 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: in run
02-21 09:17:52.232 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: in try
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: in while
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: 0
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: Start Doc
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: xpp.next
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: ET after next: 1

1 Answer 1

2
xpp.setInput(uc.getInputStream(),null);

That is the second time you use uc.getInputStream(). You already read the whole stream. You already got the whole document in String result.

You cannot read the stream a second time. You cannot read the document a second time. It will indicate the end of stream right away if you try.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, this worked! If it's not too much trouble could I ask how to parse tags with a colon in them? For example <georss:point>57.536904 -6.203359</georss:point>, I tried comparing the tag to both georss and georss:point, neither worked. Do I need to get the prefix? Or could I do .contains()?
Dont know. Never used that parser.
Can you add the code you tried? Is eventType TEXT not triggered? Or does getText() fail? What exactly is the problem?
The error I get is org.xmlpull.v1.XmlPullParserException: undefined prefix: georss (position:START_TAG <{null}georss:point>@1:831 in java.io.StringReader@8b5fbd4). The stack trace gives eventType = xpp.next(); as the line that causes the error. At first, I tried just doing tag.equalsIgnoreCase("georss") or georss:point or just point, but those all got the same error. I have tried googling solutions, and it hasn't exactly been helpful. This is the line in the XML file that seems to be causing trouble <georss:point>57.536904 -6.203359</georss:point>.
Actually ignore that, I was not reading in the first two lines of the XML file as a friend suggested, and that's where the prefix is defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.