Cannot Resolve Method getElementsByTagName

1.8k Views Asked by At

i have been trying to draw a path between to given point in android. but im getting an error saying cannot resolve method getElementsByTagName(java.lang.String) is there anything i can do get rid of this?

im getting my error in these line

nodeListStep = elementLeg.getElementsByTagName("step");

and

decodePolylines(elementStep.getElementsByTagName("points").item(0).getTextContent());

i tried doing this example http://mrbool.com/google-directions-api-tracing-routes-in-android/32001

this is my RotaTask.java

    public class RotaTask extends AsyncTask<Void, Integer, Boolean> {

    private static final String TOAST_MSG = "Calculating";
    private static final String TOAST_ERR_MAJ = "Impossible to trace Itinerary";
    private Context context; private GoogleMap gMap;
    private String editFrom; private String editTo;
    private final ArrayList<LatLng> lstLatLng = new ArrayList<LatLng>();

    public RotaTask(final Context context, final GoogleMap gMap, final String editFrom, final String editTo) {
        this.context = context;
        this.gMap= gMap;
        this.editFrom = editFrom;
        this.editTo = editTo;
    }

    /** * {@inheritDoc} */
    @Override protected void onPreExecute() {
        Toast.makeText(context, TOAST_MSG, Toast.LENGTH_LONG).show();
    }

    /*** * {@inheritDoc} */

    @Override protected Boolean doInBackground(Void... params) {
        try {
            final StringBuilder url = new StringBuilder("http://maps.googleapis.com/maps/api/directions/xml?sensor=false&language=pt"); url.append("&origin="); url.append(editFrom.replace(' ', '+')); url.append("&destination=");
            url.append(editTo.replace(' ', '+'));
            final InputStream stream = new URL(url.toString()).openStream();
            final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilderFactory.setIgnoringComments(true);
            final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            final Document document = documentBuilder.parse(stream);
            document.getDocumentElement().normalize();
            final String status = document.getElementsByTagName("status").item(0).getTextContent();

            if(!"OK".equals(status)) {
                return false;
            }

            final Element elementLeg = (Element) document.getElementsByTagName("leg").item(0);
            final NodeList nodeListStep;
            nodeListStep = elementLeg.getElementsByTagName("step");
            final int length = nodeListStep.getLength();

            for(int i=0; i<length; i++) {
                final Node nodeStep = nodeListStep.item(i);

                if(nodeStep.getNodeType() == Node.ELEMENT_NODE) {
                    final Element elementStep = (Element) nodeStep;
                    decodePolylines(elementStep.getElementByTagName("points").item(0).getTextContent());
                }
            }

            return true;
        }

        catch(final Exception e) {
            return false;
        }
    }


    private void decodePolylines(final String encodedPoints) {
        int index = 0;
        int lat = 0, lng = 0;

        while (index < encodedPoints.length()) {
            int b, shift = 0, result = 0;

            do {
                b = encodedPoints.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5;
            }

            while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat; shift = 0; result = 0;
            do {
                b = encodedPoints.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5; } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng; lstLatLng.add(new LatLng((double)lat/1E5, (double)lng/1E5));
        }
    }

    /** * {@inheritDoc} */

    @Override
    protected void onPostExecute(final Boolean result) {

        if(!result) {
        Toast.makeText(context, TOAST_ERR_MAJ, Toast.LENGTH_SHORT).show();
    }
        else {
            final PolylineOptions polylines = new PolylineOptions();
            polylines.color(Color.BLUE);

            for(final LatLng latLng : lstLatLng) {
                polylines.add(latLng);
            }

            final MarkerOptions markerA = new MarkerOptions();

            markerA.position(lstLatLng.get(0));
            markerA.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
            final MarkerOptions markerB = new MarkerOptions();
            markerB.position(lstLatLng.get(lstLatLng.size()-1));
            markerB.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
            gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lstLatLng.get(0), 10)); gMap.addMarker(markerA);
            gMap.addPolyline(polylines); gMap.addMarker(markerB);
        }
    }




}

this is acitiy_main.xml

<
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal" >

    <TextView
        android:text="From"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/editFrom"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:inputType="text"
        android:lines="1"
        android:maxLines="1" />

</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="3" android:orientation="horizontal" >

<TextView
    android:text="To"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:textStyle="bold" />

<EditText
    android:id="@+id/editTo"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:inputType="text"
    android:lines="1"
    android:maxLines="1" />

</LinearLayout>

<Button
android:id="@+id/btnGo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Go" />

</LinearLayout>
2

There are 2 best solutions below

11
On

You have a typo here

it should be,

getElementsByTagName()

and not

getElementByTagName()

According to the docs

The Element.getElementsByTagName() method returns a live HTMLCollection of elements with the given tag name.

0
On

I'm sorry for the necro, BUT, I just bumped into this issue while helping someone, and hope that if someone else finds it this will be helpful in the future.

Make sure you import the right Element class(from org.w3c.dom).enter image description here

It's such a simple issue and it's been unsolved for so long.