Groovy使用名称空间删除部分xml [英] Groovy remove part of xml using namespace

查看:145
本文介绍了Groovy使用名称空间删除部分xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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:安全性s:mustUnderstand =1xmlns:o =http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0的.xsd>
< u:时间戳u:Id =_ 0>
< u:创建> 2015-10-06T15:23:32.787Z< / u:创建>
< u:过期> 2015-10-06T15:28:32.787Z< / u:过期>
< / u:时间戳>
< / o:安全>
< / s:标题>
< 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>类型< / IDType>
< / PatientID>
< PatientID>
< ID> 45335< / ID>
< IDType> typt< / IDType>
< / PatientID>
< / PatientIDs>
< ProviderTeam>
< ProviderTeam>
< ProviderID> 1212< / ProviderID>
< ProviderIDTypes>
< IDandType>
< ID> 3543< / ID>
<类型>类型< /类型>
< / IDandType>
< IDandType>
< ID> 4535< / ID>
<类型>类型< /类型>
< / IDandType>
< IDandType>
< ID> 5353< / ID>
<类型> itype< / Type>
< / IDandType>
< IDandType>
< ID> 5353< / ID>
<类型>类型< /类型>
< / IDandType>
< / ProviderIDTypes>
< ProviderName>名称< / ProviderName>
< ProviderRole> roler< / ProviderRole>
< ProviderSpecialty />
< / ProviderTeam>
< / GetPatientTreatmentTeamResult>
< / GetPatientTreatmentTeamResponse>
< / s:Body>
< / s:信封>

我想要的是

 < CSN> 422332< / CSN> 
< PatientIDs>
< PatientID>
< ID> 23424< / ID>
< IDType>类型< / IDType>
< / PatientID>
< PatientID>
< ID> 45335< / ID>
< IDType> typt< / IDType>
< / PatientID>
< / PatientIDs>
< ProviderTeam>
< ProviderTeam>
< ProviderID> 1212< / ProviderID>
< ProviderIDTypes>
< IDandType>
< ID> 3543< / ID>
<类型>类型< /类型>
< / IDandType>
< IDandType>
< ID> 4535< / ID>
<类型>类型< /类型>
< / IDandType>
< IDandType>
< ID> 5353< / ID>
<类型> itype< / Type>
< / IDandType>
< IDandType>
< ID> 5353< / ID>
<类型>类型< /类型>
< / IDandType>
< / ProviderIDTypes>
< ProviderName>名称< / 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命名空间(http://www.w3.org/2001/XMLSchema-instance,'instance')

但我一直无法得到我想要的结果。



我会如何去做这件事

解决方案

鉴于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:页眉和GT;
| < o:安全性s:mustUnderstand =1xmlns:o =http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd> ;
| < u:时间戳u:Id =_ 0>
| < U:创建> 2015-10-06T15:23:32.787Z< / U:创建>
| < U:过期> 2015-10-06T15:28:32.787Z< / U:过期>
| < / U:时间戳>
| < / O:安全和GT;
| < /秒:抬头>
| < S:车身>
| < 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>的类型< / IDType>
| < / PatientID>
| < PatientID>
| < ID> 45335< / ID>
| < IDType> typt< / IDType>
| < / PatientID>
| < / PatientIDs>
| < ProviderTeam>
| < ProviderTeam>
| < ProviderID> 1212< / ProviderID>
| < ProviderIDTypes>
| < IDandType>
| < ID> 3543< / ID>
| <类型>的类型< /类型>
| < / IDandType>
| < IDandType>
| < ID> 4535< / ID>
| <类型>的类型< /类型>
| < / IDandType>
| < IDandType>
| < ID> 5353< / ID>
| <类型> ITYPE< /类型>
| < / IDandType>
| < IDandType>
| < ID> 5353< / ID>
| <类型>的类型< /类型>
| < / IDandType>
| < / ProviderIDTypes>
| <&的ProviderName GT;名称< /&的ProviderName GT;
| < ProviderRole> roler< / ProviderRole>
| < ProviderSpecialty />
| < / ProviderTeam>
| < / ProviderTeam>
| < / GetPatientTreatmentTeamResult>
| < / GetPatientTreatmentTeamResponse>
| < /秒:车身>
|< / s:Envelope>'''。stripMargin()

做:

  import groovy.xml。* 

new XmlParser(false,false).parseText(输入)
。'**'
.findAll {it instanceof Node}
.find {it。@'xmlns:i'=='http://www.w3.org/ 2001 / XMLSchema-instance'}
.children()
.each {println XmlUtil.serialize(it)}

打印:

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

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

<?xml version =1.0encoding =UTF-8?>< ProviderTeam>
< ProviderTeam>
< ProviderID> 1212< / ProviderID>
< ProviderIDTypes>
< IDandType>
< ID> 3543< / ID>
<类型>类型< /类型>
< / IDandType>
< IDandType>
< ID> 4535< / ID>
<类型>类型< /类型>
< / IDandType>
< IDandType>
< ID> 5353< / ID>
<类型> itype< / Type>
< / IDandType>
< IDandType>
< ID> 5353< / ID>
<类型>类型< /类型>
< / IDandType>
< / ProviderIDTypes>
< ProviderName>名称< / ProviderName>
< ProviderRole> roler< / ProviderRole>
< ProviderSpecialty />
< / ProviderTeam>
< / ProviderTeam>


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?

解决方案

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>

这篇关于Groovy使用名称空间删除部分xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆