2

groovy の XML Slurper を使用して、いくつかの xml の変更を試みています。

基本的に、私は xml を調べて、? を持つタグまたは属性を探しています。値として、それを何らかの値に置き換えます。

名前空間を持たないxmlで動作するようになりましたが、それらを含めると、物事が不安定になります。たとえば、次のようになります。

   String foo = "<xs:test xmlns:xs="http://schemas.xmlsoap.org/soap/envelope/"
      xmlns:foo="http://myschema/xmlschema" name='?'>
        <foo:tag1>?</foo:tag1>
        <foo:tag2>?</foo:tag2>
    </xs:test>";

生成:

<Envelope/>

これが私が使用しているグルーヴィーなコードです。これは、名前空間を使用していないときに機能するようです:

 public def populateRequest(xmlString, params) {

     def slurper = new XmlSlurper().parseText(xmlString)
     //replace all tags with ?
     def tagsToReplace = slurper.depthFirst().findAll{ foundTag ->
        foundTag.text() == "?"
     }.each { foundTag ->
        foundTag.text = {webServiceOperation.parameters[foundTag.name()]}
       foundTag.replaceNode{
            "${foundTag.name()}"(webServiceOperation.parameters[foundTag.name()])
        }
      }
      //replace all attributes with ?
      def attributesToReplace = slurper.list().each{
          it.attributes().each{ attributes ->
          if(attributes.value == '?')
          {
            attributes.value = webServiceOperation.parameters[attributes.key]
          }
        }
      }

      new StreamingMarkupBuilder().bind { mkp.yield slurper }.toString()
   }
4

1 に答える 1

3

グルーヴィーなドキュメントから

def wsdl = '''
<definitions name="AgencyManagementService"
    xmlns:ns1="http://www.example.org/NS1"
    xmlns:ns2="http://www.example.org/NS2">
    <ns1:message name="SomeRequest">
        <ns1:part name="parameters" element="SomeReq" />
    </ns1:message>
    <ns2:message name="SomeRequest">
        <ns2:part name="parameters" element="SomeReq" />
    </ns2:message>
</definitions>
'''

def xml = new XmlSlurper().parseText(wsdl).declareNamespace(ns1: 'http://www.example.org/NS1', ns2: 'http://www.example.org/NS2')
println xml.'ns1:message'.'ns1:part'.size()
println xml.'ns2:message'.'ns2:part'.size()
于 2010-09-03T12:23:37.900 に答える