1

I am a beginner in xPath and my XML I have items that are close (empty)

<Info>  
    <Personne>
         <field name="Name" type="String">
             <value type="String"> TOTO </value>
        </field>>
        <field name="CountryCode" type="String">
             <empty />
        </field>
    </Personne>
    <Personne>
        <field name="Name" type="String">
             <value type="String"> TOTO </value>
        </field>>
        <field name="CountryCode" type="String">
            <empty />
        </field>
    </Personne> 

ETC.......

</Info>

I can not seem to read the empty items

expression = "/Personne/field[@name='Name']/* | 
                  /Personne/field[@name='CountryCode']/*";    

NodeList nodes = (NodeList) xPath.compile(expression)
                      .evaluate(document, XPathConstants.NODESET);    

for (int i = 0; i < nodes.getLength(); i++){
    System.out.print(nodes.item(i).getFirstChild().getNodeValue());
}   

I find just Name's values in my table

And I would give to "empty" a value String like for example "null" in my table of node

  • My XML file is very big ! (1M lines ...) and aproximately 750 000 empty

  • JDom takes enormously time...

Thanks !

Jason
  • 11
  • 2
  • The dynamics of the problem indicate towards to a SAX Parser instead, where you can decide to take an action upon hitting an element `empty`. Using DOM might be cumbersome. – soufrk Apr 22 '16 at 08:12

3 Answers3

1

The nodes are selected but getNodeValue() returns "" for empty elements.

Since you used System.out.print you don't see any output for the empty elements.

Simply check if the value is empty and then convert it to a null:

String value = nodes.item(i).getFirstChild().getNodeValue();
if ("".equals(value))
     value = null;
System.out.print(value);
wero
  • 32,544
  • 3
  • 59
  • 84
  • There is no way to display even a null value when it encounters an empty value ? – Jason Apr 22 '16 at 08:12
  • I tried but I threw an exception : java.lang.NullPointerException – Jason Apr 22 '16 at 08:32
  • @Jason then do something against the NPE: http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – wero Apr 22 '16 at 08:37
0

Your XML sample contains 'Personne' nodes but your XPath expression contains 'Personnes' ... is that a misspelling? It may be worth your while to print the node name as well as its value:

System.out.print(nodes.item(i).getFirstChild().getNodeName()+":"+nodes.item(i).getFirstChild().getNodeValue());

Peter Faller
  • 132
  • 1
  • 1
  • 5
  • Thank you for the note , it is a writing error post because it is an example that I 'm writing – Jason Apr 22 '16 at 08:17
0

I would not try to access a child node if the element can be empty but rather read out the text content with e.g.

expression = "/Personne/field[@name='Name']/* | 
                  /Personne/field[@name='CountryCode']/*";    

NodeList nodes = (NodeList) xPath.compile(expression)
                      .evaluate(document, XPathConstants.NODESET);    

for (int i = 0; i < nodes.getLength(); i++){
    String value = nodes.item(i).getTextContent();
    System.out.print(value.equals("") ? "null" : value);
}   
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110