Java JDOM重复元素名称处理

Java JDOM重复元素名称处理,java,Java,比如说,我有一个这样的XML <Containers> <Container ContainerGrossWeight="0.69" ContainerGrossWeightUOM="lbs" ContainerScm="16757598166153847" TrackingNo="420913119102999949374063023016"> </Container> <Container Contai

比如说,我有一个这样的XML

    <Containers>
      <Container  ContainerGrossWeight="0.69"  ContainerGrossWeightUOM="lbs" ContainerScm="16757598166153847" TrackingNo="420913119102999949374063023016">
      </Container>
    <Container   ContainerGrossWeight="4.84" ContainerGrossWeightUOM="lbs" ContainerScm="16757598166153848" 
    TrackingNo="420913119102999949374063023016">
      </Container>
    </Containers>
上面的代码将“16757598166153847”作为输出 如何获取“16757598166153848”作为第二个元素容器属性的getAttributeValue的输出?

使用将所有名为
容器的
子容器
检索到
列表中,然后获取第二个

List<Element> containers = rootNode.getChild("Containers").getChildren("Container");
Element secondContainer = containers.get(1); // take the second one
String secondContainerSCRM = secondContainer.getAttributeValue("ContainerSCM");
List containers=rootNode.getChild(“containers”).getChildren(“Container”);
元素secondContainer=containers.get(1);//拿第二个
字符串secondContainerSCRM=secondContainer.getAttributeValue(“ContainerSCM”);
或者,您可以使用
//Containers/Container[2]
直接选择所需的元素

List<Element> containers = rootNode.getChild("Containers").getChildren("Container");
Element secondContainer = containers.get(1); // take the second one
String secondContainerSCRM = secondContainer.getAttributeValue("ContainerSCM");