yardie
yardie

Reputation: 1557

Java GetAttribute from XML code

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();
}

Upvotes: 1

Views: 805

Answers (1)

Jaydeep Patel
Jaydeep Patel

Reputation: 2423

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");;
}

Upvotes: 1

Related Questions