【问题标题】:DOM XML parsing null sub itemDOM XML 解析空子项
【发布时间】:2015-10-02 13:54:29
【问题描述】:

我正在使用带有 java 的 DOM 解析器来解析 XML 字符串。为了调试,我使用了这种递归方法:

 public static void visitRecursively(Node node) {
        // get all child nodes
    count ++;
    System.out.println("count == " + count);

    NodeList list = node.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {

        // get child node

        Node childNode = list.item(i);
    //you need to get the attributes

        NamedNodeMap attributeList = childNode.getAttributes();

      for (int j = 0; j < attributeList.getLength(); j++) {
        System.out.println("Attribute: "
                + attributeList.item(j).getNodeName() + " = "
                + attributeList.item(j).getNodeValue());
        System.out.println("Node: " + childNode.getNodeName()
        + " - with value: " + childNode.getNodeValue());

           }

        visitRecursively(childNode);
}

对应的XML是这样的:

<ObligationExpression
    ObligationId="urn:oasis:names:tc:xacml:example:obligation:emailPermit"
    FulfillOn="Permit">
    <AttributeAssignmentExpression
        AttributeId="urn:oasis:names:tc:xacml:3.0:example:attribute:text">
        <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">
            Your resource below has been
            accessed by:
        </AttributeValue>
    </AttributeAssignmentExpression>
</ObligationExpression>

我在递归调用 #2 时遇到空指针异常。

输出:

urn:oasis:names:tc:xacml:example:obligation:emailPermit
count == 1
Attribute: FulfillOn = Permit
Node: ObligationExpression - with value: null
Attribute: ObligationId = urn:oasis:names:tc:xacml:example:obligation:emailPermit
Node: ObligationExpression - with value: null
count == 2

我不知道究竟是为什么。

【问题讨论】:

  • 你希望得到什么样的输出?
  • 我编辑了问题以提高准确性

标签: java xml dom


【解决方案1】:

您正在拉动节点和它们的值,而不是属性的值。 我目前无法对此进行测试,但这可能会让你走上正轨。我稍后会修改

 public static void visitRecursively(Node node) {
  // get all child nodes
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {

    // Print text nodes if found
        Node childNode = list.item(i);
        if (childNode.getNodeName().equals("#text")) {
            System.out.print("Node name:" + node.getNodeName() + " ");
            System.out.print(childNode.getTextContent());
        }

        //you need to get the attributes
        NamedNodeMap attributeList = childNode.getAttributes();

//This was causing an issue, since there was text nodes, 
 //it was returning null and null does not have a length. 
        if (attributeList != null) {
            for (int j = 0; j < attributeList.getLength(); j++) {
                System.out.println("Attribute: "
                        + attributeList.item(j).getNodeName() + " = "
                        + attributeList.item(j).getNodeValue());
                System.out.println("Node: " + childNode.getNodeName()
                        + " - with value: " + childNode.getNodeValue());

            }
        }

        visitRecursively(childNode);

    }
}

输出:

Node name:ObligationExpression 
Attribute: AttributeId = urn:oasis:names:tc:xacml:3.0:example:attribute:text
Node: AttributeAssignmentExpression - with value: null
Node name:AttributeAssignmentExpression 
    Attribute: DataType = http://www.w3.org/2001/XMLSchema#string
Node: AttributeValue - with value: null
Node name:AttributeValue 
        Your resource below has been
        accessed by:
    Node name:AttributeAssignmentExpression 
Node name:ObligationExpression 

【讨论】:

  • 我尝试了代码:它仍然说我的元素是空的,我有点迷茫,为什么它是空的,DOM 库应该深入解析它..
  • 为什么你认为一个元素是空的? W3C DOM 中Element 节点的nodeValue 定义为null,参见w3.org/TR/DOM-Level-3-Core/core.html#ID-1950641247 中的表格
  • 好的,但是我怎样才能得到AttributeId和属性值的内容呢?
猜你喜欢
  • 2010-12-25
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 2011-05-31
  • 2013-07-24
  • 2015-04-17
  • 1970-01-01
相关资源
最近更新 更多