我不明白爲什麼XmlSlurper顯然不能處理結果。Groovy HTTPBuilder SOAP響應沒有被正確解析
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
def String WSDL_URL = ...
def http = new HTTPBuilder(WSDL_URL , ContentType.XML)
String soapEnvelope =
"""<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetTerritories xmlns="...">
<State>AZ</State>
<ZipCode>85203</ZipCode>
</GetTerritories>
</soap12:Body>
</soap12:Envelope>"""
http.request(POST, XML) {
headers."Content-Type" = "application/soap+xml; charset=utf-8"
headers."Accept" = "application/soap+xml; charset=utf-8"
body = soapEnvelope
response.success = { resp, xml ->
println "XML was ${xml}"
println "Territories were ${xml.Territories}"
println "State were ${xml.Territories.State}"
println "City was ${xml.Territories.Territory.City}"
println "County was ${xml.Territories.Territory.County}"
}
response.failure = { resp, xml ->
xml
}
}
導致
XML was <Territories><State>AZ</State><ZipCode>85203</ZipCode><Territory><City>Mesa</City><County>Maricopa</County>...</Territory></Territories>
Territories were
State were
City was
County was
UPDATE:感謝John Wagenleitner的洞察力,我做了一個小挖多。
當我再補充一點斷言,我看到一個問題:
assert "Territories" == xml.name()
| | |
| | Envelope
| <Territories><State>AZ</State><ZipCode>85203</ZipCode</Territories>
false
請求參數改變從POST, XML
到POST, TEXT
被揭示:
XML was <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetTerritoriesResponse xmlns="...">
<GetTerritoriesResult><Territories><State>AZ</State><ZipCode>85203</ZipCode><Territory><City>Mesa</City><County>Maricopa</County>...</Territory></Territories>
</GetTerritoriesResult>
</GetTerritoriesResponse>
</soap:Body>
</soap:Envelope>
...
所以看起來就像XmlSlurper一樣,當變量被打印出來時,會扔掉SOAP內容並評估最內層的節點(),而不會實際導航到該節點。這是預期的行爲?
我一直無法找到一個更完整和現代的SOAP調用和使用httpBuilder解析,所以我認爲XML將是正確的內容類型。但看起來我不得不接受TEXT並自己解析身體,這看起來很蹩腳。用httpBuilder處理SOAP響應是否有更好的方法?
謝謝,這個評論是相當有幫助的。從我讀過的Scott Davis的Groovy Recipes中,我認爲text()是用於XmlParser,而不是XmlSlurper。儘管如此,打印出來的$ {xml}和$ {xml.name()}之間的不匹配仍然受到阻礙。你知道它爲什麼這樣做嗎? –
打印出xml GPathResult時,它不顯示標籤,只顯示文本內容,所以這就是爲什麼你看到了你看到的結果。 –
如果您在GPathResult上調用text(),它將返回一個包含該元素文本內容的String。在執行println時,通常不是必須的,因爲GPathResult上的toString()方法也會打印出元素的文本內容。 –