从XML对象获取值 [英] Fetch values from XML object

查看:115
本文介绍了从XML对象获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用java获取XML对象的值。

我的XML来源

 < ; soapenv:Envelope xmlns:soapenv =http://www.w3.org/2003/05/soap-envelope> 
< soapenv:Body>
< qde:invokeResponse xmlns:qde =http://qde.service.los>
< qde:return>
< ns1:data xmlns:ns1 =http://to.service.los/xsd>
<![CDATA [<?xml version =1.0encoding =UTF-8?>
< Application>
< RefNumber> 100< / RefNumber>
< Number> 100< / Number>
< StatusCode> 142< / StatusCode>

< / Application>]]>< / ns1:data>
< ns1:errorCode xmlns:ns1 =http://to.service.los/xsd> WCP_QUERY_WS_06< / ns1:errorCode>
< ns1:errorMessage xmlns:ns1 =http://to.service.los/xsd>接口错误发生,应用程序已移至错误技术队列。< / ns1:errorMessage>
< / qde:return>
< / qde:invokeResponse>
< / soapenv:Body>
< / soapenv:Envelope>

我试图获取Application对象。



我的代码

  def rootnode = new XmlParser()。parseText(responseXml); 
def rtn = rootnode.'soapenv:Body'。'qde:invokeResponse'。'qde:return'。'ns1:data';

LOG.info(Return value iss:+ rtn);

这是打印以下内容:

  [{http://to.service.los/xsd} data [attributes = {}; 
value =
[<?xml version =1.0encoding =UTF-8?>
< Application>
< LeadRef> 100< / LeadRef>
< Number> 101< / Number>
< StatusCode> 142< / StatusCode> < / Application>]]]

我只是想抓取LeadRef,Number& StatusCode

解决方案

您需要调用 text() / p>

  def rtn = rootnode.'soapenv:Body'。'qde:invokeResponse'。'qde:return'。'ns1:data '。文本(); 
LOG.info(Return value iss:+ rtn);

这将显示以下内容:

 <?xml version =1.0encoding =UTF-8?> 
< Application>
< RefNumber> 100< / RefNumber>
< Number> 100< / Number>
< StatusCode> 142< / StatusCode>

< / Application>

现在您可以再次处理这个XML:

  def xml ='< soapenv:Envelope xmlns:soapenv =http://www.w3.org/2003/05/soap-envelope> \\\
'+
'< soapenv:Body> \\\
'+
'< qde:invokeResponse xmlns:qde =http://qde.service.los> \\\
'+
'< qde:return> \\\
'+
'< ns1:data xmlns:ns1 =http://to.service.los/xsd> \ n'+
'<![CDATA [<?xml version =1.0encoding =UTF-8?> \ n'+
'< Application> \ n'+
'< RefNumber> 100< / RefNumber> \\\
'+
'< Number> 100< / Number> \\\
'+
'< StatusCode> 142< / StatusCode> \\ \\ n'+
'\\\
'+
' < / Application>]]>< / ns1:data> \\\
'+
'< ns1:errorCode xmlns:ns1 =http://to.service.los/xsd> WCP_QUERY_WS_06< / ns1:errorCode> \ n'+
'< ns1:errorMessage xmlns:ns1 =http://to.service.los/xsd>接口错误发生,应用程序移至Error Technical Queue。< / ns1:errorMessage> \\\
'+
'< / qde:return> \\\
'+
'< / qde:invokeResponse> \ n'+
'< / soapenv:Body> \\\
'+
'< / soapenv:Envelope>'
def rootnode = new XmlParser()。parseText(xml);
def rtn = rootnode.'soapenv:Body'。'qde:invokeResponse'。'qde:return'。'ns1:data';
def application = new XmlParser()。parseText(rtn.text())
println application.'RefNumber'.text()

结果:

  100 

请参阅 http ://www.groovy-lang.org/processing-xml.html 获取更多信息。


I'm trying to fetch the values of XML object using java.

my XML source

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Body>
      <qde:invokeResponse xmlns:qde="http://qde.service.los">
          <qde:return>
               <ns1:data xmlns:ns1="http://to.service.los/xsd">
                    <![CDATA[<?xml version="1.0" encoding="UTF-8"?>
                           <Application>
                                <RefNumber>100</RefNumber>
                                <Number>100</Number>
                                <StatusCode>142</StatusCode>

                            </Application>]]></ns1:data>
                 <ns1:errorCode xmlns:ns1="http://to.service.los/xsd">WCP_QUERY_WS_06</ns1:errorCode>
        <ns1:errorMessage xmlns:ns1="http://to.service.los/xsd">Interface Error Occured, Application moved to Error Technical Queue.</ns1:errorMessage>
     </qde:return>
      </qde:invokeResponse>
   </soapenv:Body>
</soapenv:Envelope>

I'm trying to fetch Application object.

my code

def rootnode = new XmlParser().parseText(responseXml);
def rtn = rootnode.'soapenv:Body'.'qde:invokeResponse'.'qde:return'.'ns1:data';

LOG.info("Return value iss:"+rtn);

This is printing the following

 [{http://to.service.los/xsd}data[attributes={};  
     value=
         [<?xml version="1.0" encoding="UTF-8"?>
     <Application>
         <LeadRef>100</LeadRef>
    <Number>101</Number>
    <StatusCode>142</StatusCode>    </Application>]]]

I'm just trying to fetch LeadRef, Number & StatusCode

解决方案

You need to call the text() method:

def rtn = rootnode.'soapenv:Body'.'qde:invokeResponse'.'qde:return'.'ns1:data'.text();
LOG.info("Return value iss:"+rtn);

This prints the following:

<?xml version="1.0" encoding="UTF-8"?>
                       <Application>
                            <RefNumber>100</RefNumber>
                            <Number>100</Number>
                            <StatusCode>142</StatusCode>

                        </Application>

Now you can process this XML again:

def xml = '<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">\n' +
    '   <soapenv:Body>\n' +
    '      <qde:invokeResponse xmlns:qde="http://qde.service.los">\n' +
    '          <qde:return>\n' +
    '               <ns1:data xmlns:ns1="http://to.service.los/xsd">\n' +
    '                    <![CDATA[<?xml version="1.0" encoding="UTF-8"?>\n' +
    '                           <Application>\n' +
    '                                <RefNumber>100</RefNumber>\n' +
    '                                <Number>100</Number>\n' +
    '                                <StatusCode>142</StatusCode>\n' +
    '\n' +
    '                            </Application>]]></ns1:data>\n' +
    '                 <ns1:errorCode xmlns:ns1="http://to.service.los/xsd">WCP_QUERY_WS_06</ns1:errorCode>\n' +
    '        <ns1:errorMessage xmlns:ns1="http://to.service.los/xsd">Interface Error Occured, Application moved to Error Technical Queue.</ns1:errorMessage>\n' +
    '     </qde:return>\n' +
    '      </qde:invokeResponse>\n' +
    '   </soapenv:Body>\n' +
    '</soapenv:Envelope>'
def rootnode = new XmlParser().parseText(xml);
def rtn = rootnode.'soapenv:Body'.'qde:invokeResponse'.'qde:return'.'ns1:data';
def application = new XmlParser().parseText(rtn.text())
println application.'RefNumber'.text()

Result:

100

See http://www.groovy-lang.org/processing-xml.html for more info.

这篇关于从XML对象获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆