我有以下xml。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<s:Header>
<o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<u:Timestamp u:Id="_0">
<u:Created>2015-10-06T15:23:32.787Z</u:Created>
<u:Expires>2015-10-06T15:28:32.787Z</u:Expires>
</u:Timestamp>
</o:Security>
</s:Header>
<s:Body>
<GetPatientTreatmentTeamResponse xmlns="urn:Custom-com:Common.2014.Services.Patient">
<GetPatientTreatmentTeamResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<CSN>422332</CSN>
<PatientIDs>
<PatientID>
<ID>23424</ID>
<IDType>type</IDType>
</PatientID>
<PatientID>
<ID>45335</ID>
<IDType>typt</IDType>
</PatientID>
</PatientIDs>
<ProviderTeam>
<ProviderTeam>
<ProviderID>1212</ProviderID>
<ProviderIDTypes>
<IDandType>
<ID>3543</ID>
<Type>type</Type>
</IDandType>
<IDandType>
<ID>4535</ID>
<Type>type</Type>
</IDandType>
<IDandType>
<ID>5353</ID>
<Type>itype</Type>
</IDandType>
<IDandType>
<ID>5353</ID>
<Type>type</Type>
</IDandType>
</ProviderIDTypes>
<ProviderName>name</ProviderName>
<ProviderRole>roler</ProviderRole>
<ProviderSpecialty/>
</ProviderTeam>
</GetPatientTreatmentTeamResult>
</GetPatientTreatmentTeamResponse>
</s:Body>
</s:Envelope>
我想得到的是
<CSN>422332</CSN>
<PatientIDs>
<PatientID>
<ID>23424</ID>
<IDType>type</IDType>
</PatientID>
<PatientID>
<ID>45335</ID>
<IDType>typt</IDType>
</PatientID>
</PatientIDs>
<ProviderTeam>
<ProviderTeam>
<ProviderID>1212</ProviderID>
<ProviderIDTypes>
<IDandType>
<ID>3543</ID>
<Type>type</Type>
</IDandType>
<IDandType>
<ID>4535</ID>
<Type>type</Type>
</IDandType>
<IDandType>
<ID>5353</ID>
<Type>itype</Type>
</IDandType>
<IDandType>
<ID>5353</ID>
<Type>type</Type>
</IDandType>
</ProviderIDTypes>
<ProviderName>name</ProviderName>
<ProviderRole>roler</ProviderRole>
<ProviderSpecialty/>
</ProviderTeam>
我想要做的是获取节点GetPatientTreatmentTeamResult下面的文本(此节点在不同的调用中会有所不同),只使用命名空间xmlns:i =“http://www.w3.org/2001/ XMLSchema-instance(所有调用都有此)并且不使用实际的节点名。
我曾尝试使用XmlParser和XmlSlurper
def s= new XmlSlurper(false, true).parseText(xml).declareNamespace('i':'http://www.w3.org/2001/XMLSchema-instance')
def p= new XmlParser(false, true).parseText(xml)
def ns= new Namespace("http://www.w3.org/2001/XMLSchema-instance", 'instance')
但我无法得到我想要的结果。
我将如何做到这一点?
答案 0 :(得分:1)
给出xml:
grade_list=[]
valid_input=True
while valid_input:
grade= input ("Enter your grade:")
grade_list.append(grade)
else:
valid_input= false
print(grade_list)
你可以这样做:
def input = '''<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-|wssecurity-utility-1.0.xsd">
| <s:Header>
| <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
| <u:Timestamp u:Id="_0">
| <u:Created>2015-10-06T15:23:32.787Z</u:Created>
| <u:Expires>2015-10-06T15:28:32.787Z</u:Expires>
| </u:Timestamp>
| </o:Security>
| </s:Header>
| <s:Body>
| <GetPatientTreatmentTeamResponse xmlns="urn:Custom-com:Common.2014.Services.Patient">
| <GetPatientTreatmentTeamResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
| <CSN>422332</CSN>
| <PatientIDs>
| <PatientID>
| <ID>23424</ID>
| <IDType>type</IDType>
| </PatientID>
| <PatientID>
| <ID>45335</ID>
| <IDType>typt</IDType>
| </PatientID>
| </PatientIDs>
| <ProviderTeam>
| <ProviderTeam>
| <ProviderID>1212</ProviderID>
| <ProviderIDTypes>
| <IDandType>
| <ID>3543</ID>
| <Type>type</Type>
| </IDandType>
| <IDandType>
| <ID>4535</ID>
| <Type>type</Type>
| </IDandType>
| <IDandType>
| <ID>5353</ID>
| <Type>itype</Type>
| </IDandType>
| <IDandType>
| <ID>5353</ID>
| <Type>type</Type>
| </IDandType>
| </ProviderIDTypes>
| <ProviderName>name</ProviderName>
| <ProviderRole>roler</ProviderRole>
| <ProviderSpecialty/>
| </ProviderTeam>
| </ProviderTeam>
| </GetPatientTreatmentTeamResult>
| </GetPatientTreatmentTeamResponse>
| </s:Body>
|</s:Envelope>'''.stripMargin()
打印哪些:
import groovy.xml.*
new XmlParser(false, false).parseText(input)
.'**'
.findAll { it instanceof Node }
.find { it.@'xmlns:i' == 'http://www.w3.org/2001/XMLSchema-instance' }
.children()
.each { println XmlUtil.serialize(it) }