user2165673
user2165673

Reputation: 19

Groovy remove part of xml using namespace

I have the following 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>

What i would like to get is

        <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>

What i would like to do is to get the text below the node GetPatientTreatmentTeamResult (this node will be different in different calls) with only using the namespace xmlns:i="http://www.w3.org/2001/XMLSchema-instance (all call have this) and without using the actual node name.

I have tried to use XmlParser and 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')

but i haven't been able to get the result i want.

how would i go about doing this?

Upvotes: 0

Views: 1900

Answers (1)

tim_yates
tim_yates

Reputation: 171084

Given the 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()

You can do:

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) }

Which prints:

<?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>

Upvotes: 1

Related Questions