Reputation: 7
I am trying to parse some data from the following site, to an Android App which I am creating
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
Upvotes: 0
Views: 1423
Reputation: 31
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).
Upvotes: 1
Reputation: 17105
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.
Upvotes: 0