Note that there are some explanatory texts on larger screens.

plurals
  1. POIterating a NodeList consisting some tags with same name using DOM
    primarykey
    data
    text
    <p>I'm trying to read in an XML using DOM in Java</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;record&gt; &lt;user&gt; &lt;name&gt;Leo&lt;/name&gt; &lt;email&gt;****@****.com&lt;/email&gt; &lt;food-list&gt; &lt;food&gt;Hamburgers&lt;/food&gt; &lt;food&gt;Fish&lt;/food&gt; &lt;/food-list&gt; &lt;/user&gt; &lt;/record&gt; </code></pre> <p>My current solution is </p> <pre><code> for (int userNumber = 0; userNumber &lt; masterList.getLength(); userNumber++) { Node singleUserEntry = masterList.item(userNumber); if (singleUserEntry.getNodeType() == Node.ELEMENT_NODE) { org.w3c.dom.Element userEntryElement = (org.w3c.dom.Element) singleUserEntry; System.out.println("name : " + getTagValue("name", userEntryElement)); System.out.println("email : " +getTagValue("email", userEntryElement)); NodeList foodList = userEntryElement.getElementsByTagName("food-list").item(0).getChildNodes(); for(int i = 0; i &lt; foodList.getLength(); i++){ Node foodNode = foodList.item(i); System.out.println("food : " + foodNode.getNodeValue()); } private static String getTagValue(String sTag, org.w3c.dom.Element eElement) { NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes(); Node nValue = (Node) nlList.item(0); return nValue.getNodeValue(); </code></pre> <p>And the output now is</p> <pre><code>name : Leo email : ******@*****.com food : food : null food : food : null food : </code></pre> <p>Which quite confuses me. Could you tell me where I'm wrong? The number of food tags is not pre-defined.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload