Advertisement
IrvinHeslan

XMLParser.Class

May 18th, 2014
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. package com.isn.busrennes.apk;
  2. import java.net.URL;
  3.  
  4. import javax.xml.parsers.DocumentBuilder;
  5. import javax.xml.parsers.DocumentBuilderFactory;
  6.  
  7. import org.w3c.dom.Document;
  8. import org.w3c.dom.Element;
  9. import org.w3c.dom.Node;
  10. import org.w3c.dom.NodeList;
  11. import org.xml.sax.InputSource;
  12.  
  13. import android.app.Activity;
  14. import android.os.Bundle;
  15. import android.widget.LinearLayout;
  16. import android.widget.TextView;
  17.  
  18. /**
  19.  * Created by SeTioZ on 18/05/14.
  20.  */
  21. public class XMLParser  extends Activity {
  22.  
  23.         @Override
  24.         public void onCreate(Bundle savedInstanceState) {
  25.             super.onCreate(savedInstanceState);
  26.  
  27. /** Create a new layout to display the view */
  28.             LinearLayout layout = new LinearLayout(this);
  29.             layout.setOrientation(1);
  30.  
  31. /** Create a new textview array to display the results */
  32.             TextView route[];
  33.             TextView stop[];
  34.             TextView headsign[];
  35.  
  36.             try {
  37.  
  38.                 URL url = new URL(
  39.                         "http://data.keolis-rennes.com/xml/?cmd=getbusnextdepartures&version=2.1&key=FJAYVNTFKJMXHEH&param%5Bmode%5D=stopline&param%5Broute%5D%5B%5D=0077&param%5Bdirection%5D%5B%5D=0&param%5Bstop%5D%5B%5D=3115");
  40.                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  41.                 DocumentBuilder db = dbf.newDocumentBuilder();
  42.                 Document doc = db.parse(new InputSource(url.openStream()));
  43.                 doc.getDocumentElement().normalize();
  44.  
  45.                 NodeList nodeList = doc.getElementsByTagName("item");
  46.  
  47. /** Assign textview array lenght by arraylist size */
  48.                 route = new TextView[nodeList.getLength()];
  49.                 stop = new TextView[nodeList.getLength()];
  50.                 headsign = new TextView[nodeList.getLength()];
  51.  
  52.                 for (int i = 0; i < nodeList.getLength(); i++) {
  53.  
  54.                     Node node = nodeList.item(i);
  55.  
  56.                     route[i] = new TextView(this);
  57.                     stop[i] = new TextView(this);
  58.                     headsign[i] = new TextView(this);
  59.  
  60.                     Element fstElmnt = (Element) node;
  61.                     NodeList nameList = fstElmnt.getElementsByTagName("route");
  62.                     Element nameElement = (Element) nameList.item(0);
  63.                     nameList = nameElement.getChildNodes();
  64.                     route[i].setText("BUS = "
  65.                             + ((Node) nameList.item(0)).getNodeValue());
  66.  
  67.                     NodeList websiteList = fstElmnt.getElementsByTagName("stop");
  68.                     Element websiteElement = (Element) websiteList.item(0);
  69.                     websiteList = websiteElement.getChildNodes();
  70.                     stop[i].setText("stop = "
  71.                             + ((Node) websiteList.item(0)).getNodeValue());
  72.  
  73.                     headsign[i].setText("headsign = "
  74.                             + websiteElement.getAttribute("headsign"));
  75.  
  76.                     layout.addView(route[i]);
  77.                     layout.addView(stop[i]);
  78.                     layout.addView(headsign[i]);
  79.  
  80.                 }
  81.             } catch (Exception e) {
  82.                 System.out.println("XML Pasing Excpetion = " + e);
  83.             }
  84.  
  85. /** Set the layout view to display */
  86.             setContentView(layout);
  87.  
  88.         }
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement