java - 如何获取OMElement值?

标签 java xml apache axiom

OMElement.ToString()返回<DPID>0d02</DPID>但如何获得准确的值 0d02

String val = OMElement.GetText(); 

返回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 documentation getText() 是正确的方法

这是一个简单的例子:

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

输出是:

0d02

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

关于java - 如何获取OMElement值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36648892/

相关文章:

perl - 在 mod_perl 中基于 body 生成 Etag

php - php 的 apache 中的虚拟服务器和别名有什么区别?

xml - ADT 插件会自动创建一个 ant 构建文件吗?

php - Apache 不编译 php

java - selenium IDE...将输出写入外部文件

java - Codename One ConnectionRequest 中出现连接错误时的不正确行为

c# - 控制名称格式的 XSD 到 C# 类

android - 使用 holoeverywhere 库时,我的应用程序在 setcontentview 上强制关闭

java - Spring Boot 设置包含通过环境变量的配置文件

java - 字符串处理算法