Question

i need to get the driving directions from the html_instructions tag from the XML file that is returned by Google Directions. The XML file returned has hierarchy of tags as route-->leg-->step. One route tag has many leg tags and one leg tag has many step tags. Each step tag has one tag. i need to retrieve the content of all the html_instructions tag from the file through XML parsing.

i don't know how to parse through the file and get the required output in android

please help..

MY code to get the XML file:

    private void fetchData() {
    StringBuilder urlString = new StringBuilder();
    urlString
            .append("http://maps.google.com/maps/api/directions/xml?origin=");
    urlString.append(lat1);
    urlString.append(",");
    urlString.append(lon1);
    urlString.append("&destination=");// to
    urlString.append(lat2);
    urlString.append(",");
    urlString.append(lon2);
    urlString.append("&sensor=true&mode=driving");
    Log.d("url", "::" + urlString.toString());
    HttpURLConnection urlConnection = null;
    URL url = null;
    try {
        url = new URL(urlString.toString());
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.connect();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = (Document) db.parse(urlConnection.getInputStream());// Util.XMLfromString(response);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

i need to parse the document object 'doc'. Sample file returned by Google:

    <DirectionsResponse>
 <status>OK</status>
 <route>
  <summary>I-40 W</summary>
  <leg>
   <step>
    <travel_mode>DRIVING</travel_mode>
    <start_location>
     <lat>41.8507300</lat>
     <lng>-87.6512600</lng>
    </start_location>
    <end_location>
     <lat>41.8525800</lat>
     <lng>-87.6514100</lng>
    </end_location>
    <polyline>
     <points>a~l~Fjk~uOwHJy@P</points>
    </polyline>
    <duration>
     <value>19</value>
     <text>1 min</text>
    </duration>
    <html_instructions>Head <b>north</b> on <b>S Morgan St</b> toward <b>W Cermak Rd</b></html_instructions>
    <distance>
     <value>207</value>
     <text>0.1 mi</text>
    </distance>
   </step>
   ...
   ... additional steps of this leg
  ...
  ... additional legs of this route
   <duration>
    <value>74384</value>
    <text>20 hours 40 mins</text>
   </duration>
   <distance>
    <value>2137146</value>
    <text>1,328 mi</text>
   </distance>
   <start_location>
    <lat>35.4675602</lat>
    <lng>-97.5164276</lng>
   </start_location>
   <end_location>
    <lat>34.0522342</lat>
    <lng>-118.2436849</lng>
   </end_location>
   <start_address>Oklahoma City, OK, USA</start_address>
   <end_address>Los Angeles, CA, USA</end_address>
  <copyrights>Map data ©2010 Google, Sanborn</copyrights>
  <overview_polyline>
   <points>a~l~Fjk~uOnzh@vlbBtc~@tsE`vnApw{A`dw@~w\|tNtqf@l{Yd_Fblh@rxo@b}@xxSfytAblk@xxaBeJxlcBb~t@zbh@jc|Bx}C`rv@rw|@rlhA~dVzeo@vrSnc}Axf]fjz@xfFbw~@dz{A~d{A|zOxbrBbdUvpo@`cFp~xBc`Hk@nurDznmFfwMbwz@bbl@lq~@loPpxq@bw_@v|{CbtY~jGqeMb{iF|n\~mbDzeVh_Wr|Efc\x`Ij{kE}mAb~uF{cNd}xBjp]fulBiwJpgg@|kHntyArpb@bijCk_Kv~eGyqTj_|@`uV`k|DcsNdwxAott@r}q@_gc@nu`CnvHx`k@dse@j|p@zpiAp|gEicy@`omFvaErfo@igQxnlApqGze~AsyRzrjAb__@ftyB}pIlo_BflmA~yQftNboWzoAlzp@mz`@|}_@fda@jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC</points>
  </overview_polyline>
  <optimized_waypoint_index>0</optimized_waypoint_index>
  <optimized_waypoint_index>1</optimized_waypoint_index>
  <bounds>
   <southwest>
    <lat>34.0523600</lat>
    <lng>-118.2435600</lng>
   </southwest>
   <northeast>
    <lat>41.8781100</lat>
    <lng>-87.6297900</lng>
   </northeast>
  </bounds>
 </route>
</DirectionsResponse>
Was it helpful?

Solution

public ArrayList<String> getInstructions (Document doc) {
        NodeList nl1, nl2;
        ArrayList<String> listDirections = new ArrayList<String>();
        nl1 = doc.getElementsByTagName("step");
        if (nl1.getLength() > 0) {
            for (int i = 0; i < nl1.getLength(); i++) {
                Node node1 = nl1.item(i);
                nl2 = node1.getChildNodes();
                Node directionNode = nl2.item(getNodeIndex(nl2, "html_instructions"));
                String instruction = directionNode.getTextContent();
                listDirections.add(instruction.replaceAll("\\<.*?>",""));
            }
        }
        return listDirections;  
    }

OTHER TIPS

You can use 'Html.fromHtml(HTML_TEXT)'

public ArrayList<String> getInstructions (Document doc) {
    NodeList nl1, nl2;
    ArrayList<String> listDirections = new ArrayList<String>();
    nl1 = doc.getElementsByTagName("step");
    if (nl1.getLength() > 0) {
        for (int i = 0; i < nl1.getLength(); i++) {
            Node node1 = nl1.item(i);
            nl2 = node1.getChildNodes();
            Node directionNode = nl2.item(getNodeIndex(nl2, "html_instructions"));
            String instruction = directionNode.getTextContent();

            listDirections.add(Html.fromHtml(instruction));
        }
    }
    return listDirections;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top