How to read the value of the parent XML node tag in android

I have java code for reading XML nodes, I want to add it and also want to read the parent value of node.

An example of my XML file 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 xml parsing code:

 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 read the "id" value of the food, so if I search for food, it only checks those food nodes whose identifier matches the node identifier.

if I read the tag id, it reads all tags, not just one tag. How to reach it?

+4
source share
3 answers

Maybe I missed something, just String id = eElement.getAttribute("id"); which are you looking for?

You might want to learn XPath to extract from the DOM. getElementsByTagName makes it annoying to ignore the structure of a document. (The namespace and getElementsByTagNameNS should also be used :))

+4
source

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

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

This code finds all food nodes with the given identifier in the document. XPath is a rich language for such search criteria.

+4
source

XPath is available for Android from API level 8. If this is an option for you, suppose your xml is placed in your project in

/res/raw/food_list.xml

Given that you can search for all elements using this xpath syntax:

 //food/@id 

The above search expression will return a list of all id attributes related to <food> elements, wherever they have been since root.

So, if you want to get all these id attributes, you can do it like this

 InputStream is = getResources().openRawResource(R.raw.food_list); DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); try { Document doc = fac.newDocumentBuilder().parse(is); XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression expr; try { expr = xpath.compile("//food/@id"); NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); // Considering the example XML at the end of this loop you will // get the ids printed System.out.println(node.getNodeValue()); } } catch (XPathExpressionException e) { e.printStackTrace(); } is.close(); } catch (IOException e1) { e1.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } 

A quick reference to XPath syntax and a little explanation can be found here .

+3
source

Source: https://habr.com/ru/post/1309391/


All Articles