XML DOM解析,在每个getNodeValue()上获取NULL [英] XML DOM parsing, getting NULL on every getNodeValue()

查看:124
本文介绍了XML DOM解析,在每个getNodeValue()上获取NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用DOM解析XML文件。但是,即使从XML文件中可以看到,我也从节点获取NULL值,但是它们都具有值...我已经尝试了一段时间,但找不到解决该问题的方法。我是Java编程的新手,所以它可能是一个新手错误,但是现在我感到非常沮丧...

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...

这是我所使用的XML解析:

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>

我设置了一些System.out.println()来告诉我节点的名称。值,这就是我得到的:

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

我听不懂可以正常工作...这是解析XML的代码:

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!

推荐答案

元素本身实际上没有值,但是确实有一个文本节点作为子节点:

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

在元素(及其所有后代)中,使用 getTextContent() 而不是 getNodeValue()

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

这篇关于XML DOM解析,在每个getNodeValue()上获取NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆