简体   繁体   中英

How to get the value of attribute value from XML using JDOM

<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.

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

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

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

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

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.

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:

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();
}

}

To access elements that are in a namespace in JDOM, you should use the method 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/")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM