Hari Charan
Hari Charan

Reputation: 33

How to get attributes in Groovy via xpath

Facing issue in getting attribute value of xml using groovy script.

I am having a CDATA xml. I was able to parse the XML till CDATA. But, I am unable to write the XPATH for the attributes present in the Node. Please find the sample below.

NodeName=Item, attribute=ItemID

<Item ItemID="XXX-XXXXX"/>

e.g.

<Item ItemID="abc-defg"/>

groovy script:

import com.eviware.soapui.support.XmlHolder
respXmlHolder = new XmlHolder(messageExchange.getResponseContentAsXml())
respXmlHolder.declareNamespace("ns1","http://example.com/types")
CDATAXml = respXmlHolder.getNodeValue("//ns1:Response[1]/result[1]")
log.info(CDATAXml)
CDATAXmlHolder = new XmlHolder(CDATAXml)
Item = CDATAXmlHolder.getNodeValue("//ItemID")
log.info("Item = $Item")
assert '397-0109'== [email protected]()

Can someone please help me in getting the value of ItemID attribute using groovy script in SOAPUI.

Upvotes: 0

Views: 4095

Answers (1)

Hari Charan
Hari Charan

Reputation: 33

I tried this way. it worked. Thank You.

import groovy.xml.MarkupBuilder
import groovy.lang.*
import java.util.*
import com.eviware.soapui.support.UISupport

def xmlStr = """<OrderLines>
<OrderLine>
<Item ItemId='397-0109'/>
<Item ItemId='125-5449'/>
<Item ItemId='523-7449'/>
</OrderLine>
</OrderLines>"""

def xmlParse = new XmlParser().parseText( xmlStr )
def ItemId = [:]
println "Write out the Attributes for each node"
xmlParse.OrderLine.Item.each {
ItemId = it.attributes()
log.info(ItemId)
}

Upvotes: 1

Related Questions