【问题标题】:Dom parsing xml problemsdom解析xml问题
【发布时间】:2015-06-23 07:09:57
【问题描述】:

我有一个简单的.xml 文件,需要对其进行解析。文件如下:

<table name="agents">
   <row name="agent" password="pass" login="agent" ext_uid="133"/>
</table>

我需要获取name, password, login, ext_uid 的值来创建数据库记录。

我为此做了什么: 创建了一个or.w3c.dom.Document

public Document getDocument(String fileName){
        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
        f.setValidating(false);
        DocumentBuilder builder  = f.newDocumentBuilder();              
        return builder.parse(new File(fileName));
  }

接下来我正在尝试打印值:

document = getDocument(fileName);
            NodeList nodes = document.getChildNodes();
            for (int i=0; i<nodes.getLength(); i++){
                Node node = nodes.item(i);
                if(node.getNodeType() == Node.ELEMENT_NODE){
                    NodeList listofNodes = node.getChildNodes();
                    for(int j=0; j<listofNodes.getLength(); j++){
                        if(node.getNodeType() == Node.ELEMENT_NODE){
                            Node childNode = listofNodes.item(j);
                            System.out.println(childNode.getNodeValue()+" " + childNode.getNodeName());
                        }
                        }
                    }
                }

我使用它是因为我试图找出如何获取值:childNode.getNodeValue()+" " + childNode.getNodeName() 但结果如下:

#text
null row

 #text

在第一种和第三种情况下,NodeValue 为空,而在第二种情况下,它为空,这意味着,我猜根本没有 NodeValue。 所以我的问题是如何获取name, password, login, ext_uid 的值?

【问题讨论】:

    标签: java xml parsing dom


    【解决方案1】:

    childNode.getNodeValue() 显然是 null,因为它是一个空标签。你必须寻找属性

       Node childNode = listofNodes.item(j);
    
       Element e = (Element)childNode;
       String name = e.getAttribute("name");
       String password= e.getAttribute("password");
       String login= e.getAttribute("login");
       String ext_uid= e.getAttribute("ext_uid");
    

    【讨论】:

      【解决方案2】:

      &lt;row&gt; 元素没有值,它只有属性。如果它有一个值,它看起来更像&lt;row&gt;this would be the value returned from getNodeValue()&lt;/row&gt;

      获取数据的一种方法是迭代XML节点属性,例如:

      NamedNodeMap attrs = childNode.getAttributes();
      
      if (attrs != null) {
          for (int k = 0; k < attrs.getLength(); k++) {
              System.out.println("Attribute: "
                      + attrs.item(k).getNodeName() + " = "
                      + attrs.item(k).getNodeValue());
          }
      }
      

      由于示例 XML 文件中的回车符(\n 字符),您的代码输出显示 #text,根据规范,应保留该文件。示例输出中的null 是来自无值&lt;row&gt; 元素的空节点值。

      【讨论】:

        【解决方案3】:

        改用 XPath:

            XPath xp = XPathFactory.newInstance().newXPath();
        
            System.out.println(xp.evaluate("/table/row/@name", doc));
            System.out.println(xp.evaluate("/table/row/@password", doc));
            System.out.println(xp.evaluate("/table/row/@login", doc));
            System.out.println(xp.evaluate("/table/row/@ext_uid", doc));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-12-25
          • 1970-01-01
          • 1970-01-01
          • 2012-08-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多