Jonas Praem
Jonas Praem

Reputation: 2454

How to acces certain XML attributes for a given node

So I am iterating through my xml file like this:

def root = new XmlSlurper().parseText(getServiceConfigXml())

println "Project attributes: ${root.attributes()}"
root.project.each { project ->
  println "\tProject attributes: ${project.attributes()}"

  project.service.each { service ->
    println "\t\tService attributes: ${service.attributes()}"

    service.receiver.each { receiver ->
      println "\t\tReceiver: ${receiver.attributes()}"

      receiver.endpoint.each { endpoint ->
        println "\t\tEndpoint: ${endpoint.attributes()} - ${endpoint.text()}"
      }

      receiver.endpoint.each { environment ->
        println "\t\tEnvironment: ${environment.attributes()}"
      }
    }
  }
}

And here is some a example, of the XML I'm iterating through:

 <configuration>
  <service name='name' pattern='something' isReliable='maybe'>
    <receiver name='name' isUsingTwoWaySsl='maybe' isWsRmDisabled='maybe' targetedByTransformation='maybe'>
      <endpoint name='local_tst01'>URL</endpoint>
      <endpoint name='local_tst02'>URL</endpoint>
      <endpoint name='local_tst03'>URL</endpoint>
      <environment name='dev' default='local_dev' />
      <environment name='tst01' default='test' />
      <environment name='tst02' default='local_tst02' />
    </receiver>
    <operation name='name'>
      <sender>sender</sender>
      <attribute name='operation' type='String'>name</attribute>
    </operation>
  </service>
</configuration>

Take the 'receiver' node for example. Currently I'm getting an output which looks like this:

Receiver: [isWsRmDisabled:false, isUsingTwoWaySsl:true, name:CTSS, targetedByTransformation:false]

from this line of code

${receiver.attributes()}

So instead of getting all the attributes, I want to acces a specific attribute. For example the name attribute, which would look like this:

CTSS

Is this possible, without doing any sub-string workaround ?

I am imagining something like:

${receiver.name()}

This however outputs 'receiver' and not the name attribute

Upvotes: 0

Views: 25

Answers (2)

Vampire
Vampire

Reputation: 38724

What you are after is ${receiver.@name}.

Upvotes: 1

Hugues M.
Hugues M.

Reputation: 20477

${receiver.name()} gives you the tag name.

You want: ${receiver.attributes().name}

Upvotes: 1

Related Questions