0
votes

I have been trying to parse an XML which is automatically generated by google (http://maps.googleapis.com).

This is the exact XML I used as an example for my program:

http://maps.googleapis.com/maps/api/directions/xml?origin=47.066933,21.911255&destination=47.063864,21.945416&sensor=false&units=metric&mode=driving

My code:

public class MapDirection {
public final static String MODE_DRIVING = "driving";
public final static String MODE_WALKING = "walking";

public MapDirection() { }

public Document getDocument(LatLng start, LatLng end, String mode) {
    try {
        URL url = new URL("http://maps.googleapis.com/maps/api/directions/xml?" 
            + "origin=" + start.latitude + "," + start.longitude  
            + "&destination=" + end.latitude + "," + end.longitude 
            + "&sensor=false&units=metric&mode=driving");
        DocumentBuilderFactory mDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder mDocumentBuilder = mDocumentBuilderFactory.newDocumentBuilder();
        Document mDocument = mDocumentBuilder.parse(new InputSource(url.openStream()));
        mDocument.getDocumentElement().normalize();
        return mDocument;
    }
    catch (Exception e) {
        System.out.println("XML Parsing Exception = " + e);
    }

    return null;
}

public String getDurationText (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "duration"));
    NodeList nodeList4 = node3.getChildNodes();
    Node node4 = nodeList4.item(getNodeIndex(nodeList4, "text"));
    return node4.getTextContent();
}

public int getDurationValue (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "duration"));
    NodeList nodeList4 = node3.getChildNodes();
    Node node4 = nodeList4.item(getNodeIndex(nodeList4, "value"));
    return Integer.parseInt(node4.getTextContent());
}

public String getDistanceText (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "distance"));
    NodeList nodeList4 = node3.getChildNodes();
    Node node4 = nodeList4.item(getNodeIndex(nodeList4, "text"));
    return node4.getTextContent();
}

public int getDistanceValue (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "distance"));
    NodeList nodeList4 = node3.getChildNodes();
    Node node4 = nodeList4.item(getNodeIndex(nodeList4, "value"));
    return Integer.parseInt(node4.getTextContent());
}

public String getStartAddress (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "start_address"));
    return node3.getTextContent();
}

public String getEndAddress (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "end_address"));
    return node3.getTextContent();
}

public String getCopyRights (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "copyrights"));
    return node2.getTextContent();
}
private int getNodeIndex(NodeList nodeList, String nodeName) {
    for(int i = 0 ; i < nodeList.getLength() ; i++) {
        if(nodeList.item(i).getNodeName().equals(nodeName))
            return i;
    }
    return -1;
}

Example of implementation:

private void getRoute(LatLng startLocation, LatLng endLocation) {
    MapDirection mMapDirection = new MapDirection();
    Document mDocument = mMapDirection.getDocument(startLocation, endLocation, MODE_DRIVING);
    String mDuration = mMapDirection.getDurationText(mDocument);
    String mDistance = mMapDirection.getDistanceText(mDocument);
    String mStartAddress = mMapDirection.getStartAddress(mDocument);
    String mEndAddress = mMapDirection.getEndAddress(mDocument);

    Toast.makeText(getBaseContext(), "Duration: " + mDuration +
                                     "Distance: " + mDistance +
                                     "Start: " + mStartAddress +
                                     "End: " + mEndAddress, Toast.LENGTH_LONG).show();

}

My final question is, what am I doing wrong ? The errors I get:

09-13 10:52:57.791: E/AndroidRuntime(13720): FATAL EXCEPTION: main
09-13 10:52:57.791: E/AndroidRuntime(13720): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raulbutuc.taxifinder/com.raulbutuc.menu.items.OpenMap}: java.lang.NullPointerException
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.access$600(ActivityThread.java:142)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.os.Looper.loop(Looper.java:137)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.main(ActivityThread.java:4931)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at java.lang.reflect.Method.invokeNative(Native Method)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at java.lang.reflect.Method.invoke(Method.java:511)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at dalvik.system.NativeStart.main(Native Method)
09-13 10:52:57.791: E/AndroidRuntime(13720): Caused by: java.lang.NullPointerException
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.raulbutuc.GPSMapping.MapDirection.getDurationText(MapDirection.java:63)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.raulbutuc.menu.items.OpenMap.getRoute(OpenMap.java:170)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.raulbutuc.menu.items.OpenMap.onCreate(OpenMap.java:163)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.Activity.performCreate(Activity.java:5008)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139)
09-13 10:52:57.791: E/AndroidRuntime(13720):    ... 11 more
1

1 Answers

0
votes

I had the same issue too. The solution is to turn getDocument method (and all the corresponding methods) to static.