Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting attributes values from XML in Java
    primarykey
    data
    text
    <p>I am trying to parse some data from the following site, to an Android App which I am creating</p> <p><a href="http://cloud.tfl.gov.uk/TrackerNet/LineStatus/IncidentsOnly" rel="nofollow">TFL Tube Data</a></p> <p>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:</p> <pre><code>&lt;ArrayOfLineStatus&gt; - BEGINNING OF XML DOCUMENT &lt;LineStatus ID="10" StatusDetails="No service this weekend due to planned engineering work."&gt; &lt;BranchDisruptions/&gt; &lt;Line ID="7" Name="Circle"/&gt; &lt;Status ID="CS" CssClass="DisruptedService" Description="Planned Closure" IsActive="true"&gt; &lt;StatusType ID="1" Description="Line"/&gt; &lt;/Status&gt; &lt;/LineStatus&gt; &lt;ArrayOfLineStatus&gt; - END OF XML DOCUMENT </code></pre> <p>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:</p> <p><strong>Main Class</strong></p> <pre><code> 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"); } } } </code></pre> <p><strong>Handling XML</strong></p> <pre><code>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&lt;String&gt;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&lt;String&gt;(); }else if(qName.equalsIgnoreCase("Status")){ status = attributes.getValue("Status"); statuses.add(status); } } </code></pre> <p>Any Help on this would be greatly appreciated</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload