Search code examples
javaxmlapacheaxiom

How to get OMElement value?


OMElement.ToString() returns <DPID>0d02</DPID> but how to I get the exact value 0d02 ?

String val = OMElement.GetText(); 

returns java.lang.NullPointerException

I don't get it.

Added:

Here's more of my code:

OMElement elem = null;
OMNode node = null;
String text;
Iterator children = getWSIDListByDPIDList.getChildren();
while(children.hasNext()){
    node = null;
    node = (OMNode)children.next();
    if (node.getType() == OMNode.ELEMENT_NODE) 
     {
       elem = (OMElement) node;
       if (elem.getLocalName().equals("DPID"))
        {
          text = elem.getText();
        }
     }

Solution

  • According to the OMElement documentation getText() is the right method

    This is a simple example:

    String xml = "<DPID>0d02</DPID>";
    StringReader in = new StringReader(xml);
    OMElement root = OMXMLBuilderFactory.createOMBuilder(in).getDocumentElement();
    System.out.println(root.getText());
    

    And the output is:

    0d02
    

    Maybe there is something other wrong in your code.