2010-02-16 78 views
0

我之前成功地使用了XmlSlurper,但我正在努力解析以下XML - 它是对Pingdom API调用的响应。我尝试了下面的命名空间声明示例,但我只是在ns1和ns2值上收到错误消息。任何人都可以帮助我指出正确的方向吗?该XML看起来是这样的: -在Groovy/Grails中使用XmlSlurper来解析Pingdom XML响应

<?xml version="1.0" encoding="UTF-8"?> 
    <SOAP-ENV:Envelope 
     xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
     xmlns:ns1="urn:methods" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:ns2="urn:PingdomAPI" 
     xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
     SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
     <SOAP-ENV:Body> 
      <ns1:Auth_loginResponse> 
       <return xsi:type="ns2:Auth_LoginResponse"> 
        <status xsi:type="xsd:int">0</status> 
        <sessionId xsi:type="xsd:string">mysessionId</sessionId> 
       </return> 
      </ns1:Auth_loginResponse> 
     </SOAP-ENV:Body> 
    </SOAP-ENV:Envelope> 

使用它只是串接在0和mysessionId的的XmlSlurper一个字符串

0mysessionId 
+1

那真的是完整的xml吗? – Mikezx6r 2010-02-16 14:15:13

+0

,看起来并不像XML一样 – 2010-02-16 14:29:36

+0

对不起,没有把它放在代码标签! – peaky 2010-02-16 15:07:30

回答

4

试试这个,给你的XML存储在XML变量之后:

def records = new XmlSlurper().parseText(xml).declareNamespace(
    'SOAP-ENV':'http://schemas.xmlsoap.org/soap/envelope/', 
    ns1:'urn:methods', 
    xsd:'http://www.w3.org/2001/XMLSchema', 
    xsi:'http://www.w3.org/2001/XMLSchema-instance', 
    ns2:'urn:PingdomAPI' 
) 

println records.'SOAP-ENV:Body'.'ns1:Auth_loginResponse'.return.status 
println records.'SOAP-ENV:Body'.'ns1:Auth_loginResponse'.return.sessionId