Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.isn.busrennes.apk;
- import java.net.URL;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.InputSource;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- /**
- * Created by SeTioZ on 18/05/14.
- */
- public class XMLParser extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- /** Create a new layout to display the view */
- LinearLayout layout = new LinearLayout(this);
- layout.setOrientation(1);
- /** Create a new textview array to display the results */
- TextView route[];
- TextView stop[];
- TextView headsign[];
- try {
- URL url = new URL(
- "http://data.keolis-rennes.com/xml/?cmd=getbusnextdepartures&version=2.1&key=FJAYVNTFKJMXHEH¶m%5Bmode%5D=stopline¶m%5Broute%5D%5B%5D=0077¶m%5Bdirection%5D%5B%5D=0¶m%5Bstop%5D%5B%5D=3115");
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.parse(new InputSource(url.openStream()));
- doc.getDocumentElement().normalize();
- NodeList nodeList = doc.getElementsByTagName("item");
- /** Assign textview array lenght by arraylist size */
- route = new TextView[nodeList.getLength()];
- stop = new TextView[nodeList.getLength()];
- headsign = new TextView[nodeList.getLength()];
- for (int i = 0; i < nodeList.getLength(); i++) {
- Node node = nodeList.item(i);
- route[i] = new TextView(this);
- stop[i] = new TextView(this);
- headsign[i] = new TextView(this);
- Element fstElmnt = (Element) node;
- NodeList nameList = fstElmnt.getElementsByTagName("route");
- Element nameElement = (Element) nameList.item(0);
- nameList = nameElement.getChildNodes();
- route[i].setText("BUS = "
- + ((Node) nameList.item(0)).getNodeValue());
- NodeList websiteList = fstElmnt.getElementsByTagName("stop");
- Element websiteElement = (Element) websiteList.item(0);
- websiteList = websiteElement.getChildNodes();
- stop[i].setText("stop = "
- + ((Node) websiteList.item(0)).getNodeValue());
- headsign[i].setText("headsign = "
- + websiteElement.getAttribute("headsign"));
- layout.addView(route[i]);
- layout.addView(stop[i]);
- layout.addView(headsign[i]);
- }
- } catch (Exception e) {
- System.out.println("XML Pasing Excpetion = " + e);
- }
- /** Set the layout view to display */
- setContentView(layout);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement