2015-10-06 63 views
0

我有以下xml。Groovy使用命名空間刪除部分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

由於XML:

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 { [email protected]'xmlns:i' == 'http://www.w3.org/2001/XMLSchema-instance' } 
       .children() 
       .each { println XmlUtil.serialize(it) } 

它打印:

<?xml version="1.0" encoding="UTF-8"?><CSN>422332</CSN> 

<?xml version="1.0" encoding="UTF-8"?><PatientIDs> 
    <PatientID> 
    <ID>23424</ID> 
    <IDType>type</IDType> 
    </PatientID> 
    <PatientID> 
    <ID>45335</ID> 
    <IDType>typt</IDType> 
    </PatientID> 
</PatientIDs> 

<?xml version="1.0" encoding="UTF-8"?><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> 
+0

有沒有辦法讓一個XML文檔? – user2165673

+0

是的,只是連載專利節點,而不是它的孩子? Soz希望你在問題中要求的washday –