java 使用dom4j从节点获取属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12390575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:42:38  来源:igfitidea点击:

Getting attribute value from node using dom4j

javadom4j

提问by anjunatl

My XML is structured like the example below. I'm trying to get the attribute values out of XML using dom4j.

我的 XML 的结构类似于下面的示例。我正在尝试使用 dom4j 从 XML 中获取属性值。

<baz>
  <foo>
    <bar a="1" b="2" c="3" />
    <bar a="4" b="5" c="6" />
  </foo>
</baz>

Currently the nodes are stored into a List with the following code:

目前,节点存储在一个列表中,代码如下:

public List<Foo> getFoo() {
  String FOO_XPATH = "//baz/foo/*";
  List<Foo> fooList = new ArrayList<Foo>();
  List<Node> fooNodes = _bazFile.selectNodes(FOO_XPATH);

  for (Node n : fooNodes) {
    String a = /* get attribute a */
    String b = /* get attribute b */
    String c = /* get attribute c */
    fooNodes.add(new Foo(a, b, c));
  }

  return fooNodes;
}

There is a similar but different questionhere on SO but that is returning a node's value for a known attribute key/value pair using the following code:

这里有一个类似但不同的问题,但使用以下代码返回已知属性键/值对的节点值:

Node value = elem.selectSingleNode("val[@a='1']/text()");

In my case, the code knows the keys but doesn't know the values - that's what I need to store. (The above snippet from the similar question/answer also returns a node's text value when I need the attribute value.)

就我而言,代码知道键但不知道值 - 这就是我需要存储的。(当我需要属性值时,来自类似问题/答案的上述片段也会返回节点的文本值。)

回答by Jon Skeet

You have to cast the Nodeto Elementand then use the attributeor attributeValuemethods:

您必须将 to 强制Node转换Element,然后使用attributeorattributeValue方法:

for (Node node : fooNodes) {
    Element element = (Element) node;
    String a = element.attributeValue("a");
    ...
}

Basically, getting the attribute value from "any node" doesn't make sense, as some node types (attributes, text nodes) don't haveattributes.

基本上,从“任一节点”得到的属性值是没有意义的,因为一些节点类型(属性,文本节点)不具备的属性。

回答by Ami

public List<Foo> getFoo() {
  String FOO_XPATH = "//baz/foo/*";
  List<Foo> fooList = new ArrayList<Foo>();
  List<Node> fooNodes = _bazFile.selectNodes(FOO_XPATH);

  for (Node n : fooNodes) {
    Element element = (Element) n;
    String a = element.attributeValue("a");
    String b = element.attributeValue("b");
    String c = element.attributeValue("c");
    fooNodes.add(new Foo(a, b, c));
  }

  return fooNodes;
}

I think you need to convert node into element then only its works fine.

我认为您需要将节点转换为元素,然后才能正常工作。

回答by Jay Shark

You can also use xpath to get the value of a node attribute -

您还可以使用 xpath 来获取节点属性的值 -

  for (Node n : fooNodes) {
    String a = n.valueOf("@a");
    String b = n.valueOf("@b");
    String c = n.valueOf("@c");
    fooNodes.add(new Foo(a, b, c));
  }