从XML对象获取值

时间:2015-04-21 10:51:09

标签: xml groovy

我试图使用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.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>

我试图获取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.0" encoding="UTF-8"?>
     <Application>
         <LeadRef>100</LeadRef>
    <Number>101</Number>
    <StatusCode>142</StatusCode>    </Application>]]]

我只是想获取LeadRef,Number&amp;的StatusCode

1 个答案:

答案 0 :(得分:1)

您需要调用text()方法:

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

这将打印以下内容:

<?xml version="1.0" encoding="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">\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()

结果:

100

有关详细信息,请参阅http://www.groovy-lang.org/processing-xml.html