XML DOM parsing, getting NULL on every getNodeValu

2019-08-08 09:26发布

问题:

I'm trying to parse an XML file using DOM. But I'm getting NULL values from the nodes, even though, as you can see in the XML file, they all have values... I've been trying for a while but can't find a solution to this issue. I'm quite a newbie to java programming, so it's probably a newbie error, but i'm getting quite frustrated now...

This is the XML i'm parsing:

<root>
  <seguro>
      <seg_tipo>cocacola</seg_tipo> 
      <seg_subtipo>cocacola</seg_subtipo> 
      <seg_title>cocacola</seg_title> 
      <seg_descr>cocacola</seg_descr> 
      <seg_carac_title>cocacola</seg_carac_title> 
      <seg_carac>cocacola</seg_carac> 
  </seguro>
  <seguro>
     <seg_tipo>fantanaranja</seg_tipo> 
     <seg_subtipo>fantanaranja</seg_subtipo> 
     <seg_title>fantanaranja</seg_title> 
     <seg_descr>fantanaranja</seg_descr> 
     <seg_carac_title>fantanaranja</seg_carac_title> 
     <seg_carac>fantanaranja</seg_carac> 
  </seguro>
</root>

I've set some System.out.println() to tell me the name of the node an the value, and this is what i get:

seg_tipo -- null
seg_subtipo -- null
seg_title -- null
seg_descr -- null
seg_carac_title -- null
seg_carac -- null
/n-------------------------------------/n
seg_tipo -- null
seg_subtipo -- null
seg_title -- null
seg_descr -- null
seg_carac_title -- null
seg_carac -- null
/n-------------------------------------/n

I can't get it working... this is the code that parses he XML:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                File fileXML = new File("seguros.xml");
                Document document = builder.parse(fileXML);

                Element root = document.getDocumentElement();
                NodeList items = root.getElementsByTagName("seguro");
//                System.out.println(items.getLength());
//                System.out.println(items.item(1).getNodeName());

                for (int i = 0; i < items.getLength(); i++) {
                    Seguro seguroActual = new Seguro();
                    Node item = items.item(i);
                    NodeList datosSeguro = item.getChildNodes();
                    for (int j = 0; j < datosSeguro.getLength(); j++) {
//                        System.out.println(datosSeguro.getLength());
                        Node dato = datosSeguro.item(j);
                        String etiqueta = dato.getNodeName();
                        System.out.println(dato.getNodeName()+" -- "+dato.getNodeValue());

                        if (etiqueta.equals("seg_tipo")) {
                            seguroActual.setSeg_tipo(dato.getNodeValue());
//                            System.out.println("El tipo del seguro es: " + dato.getNodeValue());
                        } else if (etiqueta.equals("seg_subtipo")) {
                            seguroActual.setSeg_subtipo(dato.getNodeValue());
//                            System.out.println("El subtipo del seguro es: " + seguroActual.getSeg_subtipo());
                        }
                    }
                    System.out.println("/n-------------------------------------/n");
                    AlmacenSeguros.getInstance().guardarSeguro(seguroActual);
                }

It probably is a very simple problem, but can't find the answer... :( thanks in advance!

回答1:

The element itself does not actually have a value, but it does have a text node as a child:

<root>
  <seguro>
    <seg_tipo>
      #text: cocacola

To get the text in an element (and all of its descendants), use getTextContent() rather than getNodeValue().