2 Replies Latest reply: Dec 24, 2009 8:22 AM by 807580 RSS

    Problem with getNodeValue() - simple Java XPath question

    807580
      I am trying to extract the node values from a XML file:
      <root>
        <frame>
           <boxes>
             <box>
                <spec>22</spec>
                <spec>2222</spec>
             </box>
             <box>
                <spec id="BA" value="short"/>
                <spec id="BB" value="thin"/>
                <spec id="BC" value="black"/>
                <spec id="BD" value="full"/>
                <spec id="BE" value="7"/>
                <spec id="BF" value="deactive"/>
             </box>                 
           </boxes>
           <circles>
             <circle id="CA" value="blue"/>
             <circle id="CB" value="green"/>
           </circles>
        </frame>
      </root>
      I use this code:
      ...
      ...
      
               XPathFactory factory = XPathFactory.newInstance();
               XPath xpath = factory.newXPath();
               XPathExpression xPathExpression = xpath.compile("/root/frame/boxes/box/spec");             
      
               Object result = xPathExpression.evaluate(doc, XPathConstants.NODESET);
               NodeList nodes = (NodeList) result;
               for (int i = 0; i < nodes.getLength(); i++) {
                   System.out.println(nodes.item(i).getNodeValue());
               }
      But only:

      null
      null
      null
      null
      null
      null
      null
      null

      get printed. I would have assumed:

      22
      2222
      null
      null
      null
      null
      null
      null

      since the two first spec nodes contains the above numbers. Any ideas?