如何使用JDOM从XML获取属性值的值

时间:2022-11-27 07:58:55
<ns2:VehicleStatusReport>
   <ns2:DataContent>
      <ns2:DataItem>
         <ns2:Name>star</ns2:Name>
         <ns2:Percent>65.6</ns2:Percent>
         <ns2:Source>egg</ns2:Source>
      </ns2:DataItem>

     <ns2:DataItem>
         <ns2:Name>position</ns2:Name>
         <ns2:Position>
            <ns3:RandomNumbers>8.378137,8.36</ns3:RandomNumbers>                       
            <ns3:Integer>124.9,124.999901</ns3:Integer>
            <ns3:Heading>0</ns3:Heading>          
         </ns2:Position>             
      </ns2:DataItem>

Above is sample XML that I'm trying to parse in java using JDOM, following is my code for the same.

上面是我尝试使用JDOM在java中解析的示例XML,以下是我的相同代码。

String xml="above xml";
org.jdom.Document doc = saxBuilder.build(new StringReader(xml));
Element rootNode = doc.getRootElement();
Element subRootNode = rootNode.getChild("ns2:DataContent");
Element subsubRootNode =  subRootNode.getChild("ns2:DataItem");
Element subsubsubRootNode = subsubRootNode.getChild("ns2:Position");
String value=subsubsubRootNode.getAttributeValue("ns3:RandomNumbers");
System.out.println(value);

But When I execute the above code I get NULLPOINTEREXCEPTION at follwing line

但是当我执行上面的代码时,我会在后面的行中获得NULLPOINTEREXCEPTION

Element subsubRootNode =  subRootNode.getChild("ns2:DataItem");

How to get the value of RandomNumbers and Interger xml tags? Thanks !!!!

如何获取RandomNumbers和Interger xml标签的值?谢谢 !!!!

2 个解决方案

#1


-1  

To access elements that are in a namespace in JDOM, you should use the method getChild(localName, namespaceUri).

要访问JDOM中名称空间中的元素,应使用方法getChild(localName,namespaceUri)。

For example if the element is

例如,如果元素是

<ns:donald xmlns:ns="http://us.nepotism.com/">
  <ns:ivanka/>
</ns:donald>

then you should use getChild("ivanka", "http://us.nepotism.com/")

那么你应该使用getChild(“ivanka”,“http://us.nepotism.com/”)

The answer from @GalDraiman appears to ignore the namespace issue completely.

来自@GalDraiman的答案似乎完全忽略了命名空间问题。

#2


-1  

You get NullPointerException because you try to get child of non-existing sub-object:

您得到NullPointerException,因为您尝试获取不存在的子对象的子项:

When performing

Element subsubRootNode =  subRootNode.getChild("ns2:DataItem");

The return value is the first node which does not include the ns2:Position, as opposed to the another ns2:DataItem you have in your example.

返回值是第一个不包含ns2:Position的节点,而不是您示例中的另一个ns2:DataItem。

Anyway, a simple solution I suggest is to use getChildren instead of getChild when you have a list of multiple nodes with the same attribute:

无论如何,我建议一个简单的解决方案是当你有一个具有相同属性的多个节点的列表时使用getChildren而不是getChild:

String xml="above xml";
org.jdom.Document doc = saxBuilder.build(new StringReader(xml));
Element rootNode = doc.getRootElement();
List dataItemsList = rootNode.getChildren("ns2:DataContent");
for ( int i = 0; i < dataItemsList.size(); i++ ) {
    Element dataItem = (Element) dataItemsList.get(i);
    Element position = subsubRootNode.getChild("ns2:Position");
    if(position != null){
         String value=subsubsubRootNode.getAttributeValue("ns3:RandomNumbers");
         System.out.println(value);
    }
}

EDIT: Full code:

String xml = "<VehicleStatusReport>" + 
                "<DataContent>" + 
                    "<DataItem>" + 
                        "<Name>star</Name>" + 
                        "<Percent>65.6</Percent>" + 
                        "<Source>egg</Source>" + 
                    "</DataItem>" +

                    "<DataItem>" + 
                        "<Name>position</Name>" + 
                        "<Position>" + 
                            "<RandomNumbers>8.378137,8.36</RandomNumbers>" + 
                            "<Integer>124.9,124.999901</Integer>" + 
                            "<Heading>0</Heading>" + 
                        "</Position>" + 
                    "</DataItem>" + 

                "</DataContent>" + 
            "</VehicleStatusReport>";

SAXBuilder saxBuilder = new SAXBuilder();
Document doc;
try {
    doc = saxBuilder.build(new StringReader(xml));
    Element rootNode = doc.getRootElement();
    Element dataContent = rootNode.getChild("DataContent");
    List dataItemsList = dataContent.getChildren();
    for (int i = 0; i < dataItemsList.size(); i++) {
        Element dataItem = (Element) dataItemsList.get(i);
        Element position = dataItem.getChild("Position");
        if (position != null) {
            Element randomNumbers = position.getChild("RandomNumbers");
            List value = randomNumbers.getContent();
            System.out.println(value.get(0));
        } 
    }
} catch (JDOMException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

#1


-1  

To access elements that are in a namespace in JDOM, you should use the method getChild(localName, namespaceUri).

要访问JDOM中名称空间中的元素,应使用方法getChild(localName,namespaceUri)。

For example if the element is

例如,如果元素是

<ns:donald xmlns:ns="http://us.nepotism.com/">
  <ns:ivanka/>
</ns:donald>

then you should use getChild("ivanka", "http://us.nepotism.com/")

那么你应该使用getChild(“ivanka”,“http://us.nepotism.com/”)

The answer from @GalDraiman appears to ignore the namespace issue completely.

来自@GalDraiman的答案似乎完全忽略了命名空间问题。

#2


-1  

You get NullPointerException because you try to get child of non-existing sub-object:

您得到NullPointerException,因为您尝试获取不存在的子对象的子项:

When performing

Element subsubRootNode =  subRootNode.getChild("ns2:DataItem");

The return value is the first node which does not include the ns2:Position, as opposed to the another ns2:DataItem you have in your example.

返回值是第一个不包含ns2:Position的节点,而不是您示例中的另一个ns2:DataItem。

Anyway, a simple solution I suggest is to use getChildren instead of getChild when you have a list of multiple nodes with the same attribute:

无论如何,我建议一个简单的解决方案是当你有一个具有相同属性的多个节点的列表时使用getChildren而不是getChild:

String xml="above xml";
org.jdom.Document doc = saxBuilder.build(new StringReader(xml));
Element rootNode = doc.getRootElement();
List dataItemsList = rootNode.getChildren("ns2:DataContent");
for ( int i = 0; i < dataItemsList.size(); i++ ) {
    Element dataItem = (Element) dataItemsList.get(i);
    Element position = subsubRootNode.getChild("ns2:Position");
    if(position != null){
         String value=subsubsubRootNode.getAttributeValue("ns3:RandomNumbers");
         System.out.println(value);
    }
}

EDIT: Full code:

String xml = "<VehicleStatusReport>" + 
                "<DataContent>" + 
                    "<DataItem>" + 
                        "<Name>star</Name>" + 
                        "<Percent>65.6</Percent>" + 
                        "<Source>egg</Source>" + 
                    "</DataItem>" +

                    "<DataItem>" + 
                        "<Name>position</Name>" + 
                        "<Position>" + 
                            "<RandomNumbers>8.378137,8.36</RandomNumbers>" + 
                            "<Integer>124.9,124.999901</Integer>" + 
                            "<Heading>0</Heading>" + 
                        "</Position>" + 
                    "</DataItem>" + 

                "</DataContent>" + 
            "</VehicleStatusReport>";

SAXBuilder saxBuilder = new SAXBuilder();
Document doc;
try {
    doc = saxBuilder.build(new StringReader(xml));
    Element rootNode = doc.getRootElement();
    Element dataContent = rootNode.getChild("DataContent");
    List dataItemsList = dataContent.getChildren();
    for (int i = 0; i < dataItemsList.size(); i++) {
        Element dataItem = (Element) dataItemsList.get(i);
        Element position = dataItem.getChild("Position");
        if (position != null) {
            Element randomNumbers = position.getChild("RandomNumbers");
            List value = randomNumbers.getContent();
            System.out.println(value.get(0));
        } 
    }
} catch (JDOMException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}