1

I am trying to use the getAttribute java function with this code. In the xml I have a something like this

  <city id="1" name="John Doe">

and I want to get the "name" attribute:

I searched the forum and found a few topics about it already but I tried a few and cant seem to get it right, a little help would be appreciated, thanks.

this is my code:

   protected void onPostExecute(Void args) {

        for (int temp = 0; temp < nodelist.getLength(); temp++) {
            Node nNode = nodelist.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;

                textview.setText(textview.getText() + "Name : "
                        + getNode("city", eElement)+ "\n" + "\n");
            }
        }
        // Close progressbar
        pDialog.dismiss();
    }
}

// getNode function
private static String getNode(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

    Node nValue = (Node) nlList.item(0);
    return nValue.getNodeValue();
}

1 Answer 1

1

You should use Element.getAttribute

private static String getNode(String sTag, Element eElement) {
    Element e = (Element) eElement.getElementsByTagName(sTag).item(0);
    return e.getAttribute("name");;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.