代码之家  ›  专栏  ›  技术社区  ›  Stefan Kendall

XOM从节点获取属性?

  •  0
  • Stefan Kendall  · 技术社区  · 15 年前

    像这样的东西不管用吗?

    假设文档的格式如下:

    <root>
       <element id = "a"></element>
    </root>
    
    Node node = doc.query("/root/element").get(0);
    String id = node.getDocument().getRootElement().getAttribute("id");
    

    当我打印根元素的值时,看起来这应该是可行的。怎么了,这里?

    2 回复  |  直到 15 年前
        1
  •  2
  •   Stefan Kendall    15 年前

        2
  •  0
  •   Kannan Ekanath    15 年前

    node.getDocument().getRootElement()此时您拥有的元素没有属性“id”。

    改为尝试node.getAttribute(“id”)吗(假设节点不为空)

    推荐文章