0

I am trying to parse some data from the following site, to an Android App which I am creating

TFL Tube Data

The data in this feed is constantly updated and can hold anything from nothing to 15/16 items. The basic format of each item is as following:

<ArrayOfLineStatus> - BEGINNING OF XML DOCUMENT    
    <LineStatus ID="10" StatusDetails="No service this weekend due to planned engineering work.">
        <BranchDisruptions/>
        <Line ID="7" Name="Circle"/>
        <Status ID="CS" CssClass="DisruptedService" Description="Planned Closure" IsActive="true">
            <StatusType ID="1" Description="Line"/>
        </Status>
    </LineStatus>
<ArrayOfLineStatus> - END OF XML DOCUMENT

I need to go through the entire and pull the value of the attribute of "Name" in Line and "Description" in "Status". So in the above I would be pulling "Circle" and "Planned Closure". The classes I have made so far is as follows:

Main Class

    public class TubeStatusXMLParsing  extends Activity {

        static final String baseURL = "http://cloud.tfl.gov.uk/TrackerNet    
                                          /LineStatus/IncidentsOnly";
       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle icicle) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build(); 
            StrictMode.setThreadPolicy(policy);
            super.onCreate(icicle);
            setContentView(R.layout.tube_status);
            getStatus();        
            }

       public void getStatus() {
        // TODO Auto-generated method stub
        try{
            URL website = new URL(baseURL);
            //getting xmlreader to parse data
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            HandlingXMLStuff doingWork = new HandlingXMLStuff();
            xr.setContentHandler(doingWork);
            xr.parse(new InputSource(website.openStream()));
            String listofStatuses = doingWork.getInformation();
            circleStatus.setText(listofStatuses);
        }catch (Exception e){
            circleStatus.setText("error");
        }
    }
}

Handling XML

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.sax.Element;
import android.sax.RootElement;

public class HandlingXMLStuff extends DefaultHandler {

    private ArrayList<String>statuses = null;
    String status;
    String lineName;

    public String getInformation(){
        return statuses.get(0);
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        if(qName.equalsIgnoreCase("ArrayOfLineStatus")){
            statuses = new ArrayList<String>();
        }else if(qName.equalsIgnoreCase("Status")){
            status = attributes.getValue("Status");
            statuses.add(status);
            }

        }

Any Help on this would be greatly appreciated

Thanks

6
  • What is the actual question? Doesn't it work? Does it throw any exception? Commented Feb 3, 2013 at 19:02
  • 2
    I don't see the attribute named "Status" in xml Commented Feb 3, 2013 at 19:03
  • @NikitaBeloglazov the code doesn't work, when I try and view run it on my phone the textview is blank, where as it should display "Planned Closure". Commented Feb 3, 2013 at 19:07
  • Status, is the 5th line in the XML Code Commented Feb 3, 2013 at 19:07
  • 1
    Try to replace getValue("Status") with getValue("Description"). Commented Feb 3, 2013 at 19:15

2 Answers 2

1

You're on the right track using the Attributes object, but you have to use the index position of the attribute within the element.

Hence you need to iterate through the attributes of the element using a for loop on the attributes.getName() method until attributes.getName(i).equals("Name"). Once you determine the index, just use attribute attributes.getValue(i).

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

Comments

0

It is hard to understand what you need, but you are getting the attributes wrong. I think it should somehow like this

else if (qName.equalsIgnoreCase("Status")) {
    object = new Status();
    object.id = attributes.getValue("ID");
    object.cssClass = attributes.getValue("CssClass");
    object.isActive = attributes.getValue("IsActive");
    object.description = attributes.getValue("Description");
}
else if (qName.equalsIgnoreCase("StatusType")) {
    object.typeId = attributes.getValue("ID");
    //etc, and maybe the StatusType should be a nested class of Status and stored in array field
}

And in your endElement

if (qName.equalsIgnoreCase("Status")) {
    statuses.add(object);
}

I'm too lazy to write the whole thing, I wrote it just to show how to get attributes. Also, I prefer using android.sax instead of directly creating the DefaultHandler.

3 Comments

He actually needs "Description" attribute as I understood from his comment.
correct, I need the "Description" attribute from the "Status" tag and the "Name" attribute from the "Line" tag
Ok, updated, but it was obvious from the answer how to get the description - just like the other attributes by name :)

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.