I want to extract the data between 2 nodes Insu and /Insu from the below XML without namespaces in output
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<Qres xmlns="http://www.test.com/h/xsd">
<PMR xmlns:xsd="http://www.test.com/h/xsd">
<ID xmlns="http://example.services/Results.xsd" xmlns:ns1="urn:Group/20160505/Ons" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1159</ID>
<ref xmlns="http://example.services/Results.xsd" xmlns:ns1="urn:Group/20160505/Ons" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">12345</ref>
<SingleQres xmlns="http://example.services/Results.xsd" xmlns:ns1="urn:Group/20160505/AddOns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Qres>
<ref>12345</ref>
<SC>ok</SC>
<Sche>
<csc>Car</csc>
<Insu>
<Entry>
<Key>ok</Key>
<Value>hello</Value>
<MetData>
<Key>test</Key>
<Value>test</Value>
</MetData>
</Entry>
</Insu>
</Sche>
</Qres>
</SingleQres>
</PMR>
</Qres>
I wrote the below code and able to get it. Years is the name of the test step whose result is above XML
def c=context.expand('${Years#Response#//*:Qres//*:Sche//*:Insu/*}')
log.info c
The output is below
<res:Entry xmlns:res="http://example.services/Results.xsd">
<res:Key>ok</res:Key>
<res:Value>hello</res:Value>
<res:MetData>
<res:Key>test</res:Key>
<res:Value>test</res:Value>
</res:MetData>
</res:Entry>
Problem :- It has namespace appended to it.
I wanted XML to be without , only nodes should be there like below as it was there in original XML
<Entry>
<Key>ok</Key>
<Value>hello</Value>
<MetData>
<Key>test</Key>
<Value>test</Value
It would be great if using the above command with some tweak i can get the nodes without namespaces as i want to use those values in next steps without namespace prefixed
You can use XmlSlurper
without namespace, and XmlUtil
to serialize as shown below:
def xml = new XmlSlurper(false, false).parseText(context.expand('${Years#Response}'))
log.info groovy.xml.XmlUtil.serialize(xml.'**'.find{it.name() == 'Insu'}.children())
You can quickly try the same online demo
EDIT: based on OP comment.
XmlNodePrinter
be used to get it without xml markup declaration.
def xml = new XmlParser(false, false).parseText(context.expand('${Years#Response}'))
def entry = xml.'**'.find{it.name() == 'Entry'}
def sw = new StringWriter()
new XmlNodePrinter(new PrintWriter(sw)).print(entry)
log.info sw.toString()
Here is the demo for the same.