如何获取 OMElement 值?

How to get OMElement value?

OMElement.ToString() returns <DPID>0d02</DPID> 但是如何获得确切的值 0d02

String val = OMElement.GetText(); 

returns java.lang.NullPointerException

我不明白。

已添加:

这是我的更多代码:

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

按照OMElement documentationgetText()是正确的方法

这是一个简单的例子:

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

输出为:

0d02

也许您的代码中还有其他问题。