Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read XML parent node tag value in android

I have a java code to read XML nodes, I want to add in the addition and want to read the parent node value also.

my XML file sample is below:

 <breakfast_menu><food id=1><name> Belgian Waffles </name><price> $5.95 </price><description> two of our famous Belgian Waffles with plenty of real maple syrup </description><calories> 650 </calories></food><food id=2><name>Strawberry Belgian waffles</name><price>$7.95</price><description>light Belgian waffles covered with strawberries and whipped cream</description><calories>900</calories></food></breakfast_menu>

and my code for parsing xml is :

public static String getProductItem(String pid, String item) {

    try {
        url = new URL("");
        urlConnection = url.openConnection();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {

    }

    try {
        dBuilder = dbFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
    }
    try {
        doc = dBuilder.parse(urlConnection.getInputStream());
    } catch (SAXException e) {

    } catch (IOException e) {

    }

    doc.getDocumentElement().normalize();

    NodeList nList = doc.getElementsByTagName("food");

    for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

            Element eElement = (Element) nNode;



                data = getTagValue(item, eElement);



        }
    }

    doc = null;
    dBuilder = null;

    return data;

}


private static String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
    .getChildNodes();
    Node nValue = (Node) nlList.item(0);

    return nValue.getNodeValue();
}

What I want to do is to read the "id" value of food, so if if I am searching for a food, it only checks those food nodes, whose id matched the food node id.

if I read by tag id, it reads all tags not specifically one tag. How to achieve it?

like image 407
Adil Bhatty Avatar asked Dec 22 '22 03:12

Adil Bhatty


2 Answers

Maybe I missed somthing, is it just String id = eElement.getAttribute("id"); that you're looking for?

You may want to look into using XPath to fetch out of your DOM. getElementsByTagName has an irritating habit of ignoring the document structure. (Also ought to be using a namespace and getElementsByTagNameNS :) )

like image 158
Affe Avatar answered Dec 24 '22 16:12

Affe


You can use XPath to easily find a set of nodes according to some criteria. For example:

XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) xpath.evaluate("//food[@id=22]", doc, XPathConstants.NODESET);

This code finds all food nodes with a given id in the document. XPath is a rich language for this kind of search criteria.

like image 24
Eyal Schneider Avatar answered Dec 24 '22 16:12

Eyal Schneider