我是groovy的新手。Groovy xml請求解析
試圖解析一些XML請求,沒有運氣一段時間。
至於最終結果:
- 我要檢查,如果XML請求 「RequestRecords」 有 「DetailsRequest」 atrribute;
- 獲得「FieldValue」號碼,其中「RequestF」具有FieldName =「Id」。
此外,由於某種原因,因爲它返回false爲 'DEF根=新XmlParser的()。parseText(XML)' 我不能使用的XmlSlurper。
def env = new groovy.xml.Namespace("http://schemas.xmlsoap.org/soap/envelope/", 'env');
def ns0 = new groovy.xml.Namespace("http://tempuri.org/", 'ns0')
def xml = '''<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns0="http://tempuri.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body>
<ns0:Request1>
<ns0:Request_Sub>
<ns0:RequestRecords TableName="Header">
<ns0:RequestF FieldName="SNumber" FieldValue="XXX"/>
<ns0:RequestF FieldName="TNumber" FieldValue="30"/>
</ns0:RequestRecords>
<ns0:RequestRecords TableName="Details">
<ns0:RequestF FieldName="Id" FieldValue="1487836040"/>
</ns0:RequestRecords>
</ns0:Request_Sub>
<ns0:isOffline>false</ns0:isOffline>
</ns0:Request1>
</env:Body>
</env:Envelope>'''
def root = new XmlParser().parseText(xml)
println ("root" + root)
assert "root_node" == root.name()
println root_node
根節點即使斷言失敗。
那麼,正確的說法應該是在這種情況下? assert root.name()=='Envelope' 也失敗。 –
在斷言之前添加'println'root [$ {root.name}]''以驗證它是什麼很容易。它爲我顯示了「信封」。 –