繁体   English   中英

在Groovy / Grails中使用XmlSlurper解析Pingdom XML响应

[英]Using XmlSlurper in Groovy / Grails to parse a Pingdom XML response

我以前已经成功使用过XmlSlurper,但是正在努力解析以下XML-这是对Pingdom API调用的响应。 我已经尝试按照名称空间声明示例进行操作,但是我只是收到关于ns1和ns2值的错误消息。 有人可以帮我指出正确的方向吗? 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>

使用XmlSlurper之后,它只是将0和mysessionId连接到一个字符串

0mysessionId

试试这个,让您的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM