XML Handling using X-path using Java/groovy script - groovy

I have a very deep/nested structure xml where I need to filter only those fields which will be declared in some store(parameter let's say). Or the other solution is , I need to make those field values as "X" which are not stored in the parameters. As the structure is very deep, I suppose the field should be declared in the parameter as x-path.
Please suggest some clues.
I am using the below code now but this is removing all the fields under different nodes as well. I want to remove this from a particular node. For example(as shown below) field 'action' is present in many nodes but I want to remove this from some specific nodes.
Or I want to set this field value as "X".
def xml = new XmlParser().parseText(body)
def nodes = xml.'**'.findAll{ node -> node.name() == 'action' }
//nodes.each{it.text()}
def removeNode = { node ->
def parent = node.parent()
parent.remove(node)
}
nodes.each{removeNode(it)}
def final_body = groovy.xml.XmlUtil.serialize(xml);
My input data is as follows:
<?xml version='1.0' encoding='UTF-8'?>
<query>
<Emp>
<id>34032</id>
<ps>
<action>INSERT</action>
<id>34032</id>
<PI>
<action>INSERT</action>
<gen>M</gen>
<ms>S</ms>
<PI_mex>
<action>INSERT</action>
<genericNumber1>0</genericNumber1>
<genericNumber2>0</genericNumber2>
<genericNumber3>0</genericNumber3>
<genericNumber4>0</genericNumber4>
<genericNumber5>0</genericNumber5>
<genericNumber6>0</genericNumber6>
<genericNumber7>0</genericNumber7>
</PI_mex>
</PI>
<PI>
<action>INSERT</action>
<gen>M</gen>
<ms>S</ms>
<PI_mex>
<action>INSERT</action>
<genericNumber1>0</genericNumber1>
<genericNumber2>0</genericNumber2>
<genericNumber3>0</genericNumber3>
<genericNumber4>0</genericNumber4>
<genericNumber5>0</genericNumber5>
<genericNumber6>0</genericNumber6>
<genericNumber7>0</genericNumber7>
</PI_mex>
</PI>
<ai>
<action>INSERT</action>
<city>SAN NICOALAS DE LOS GARZA</city>
<country>MEX</country>
<state>NLE</state>
<zip_code>66420</zip_code>
</ai>
<ai>
<action>INSERT</action>
<city>SAN NICOALAS DE LOS GARZA</city>
<country>MEX</country>
<state>NLE</state>
<zip_code>66420</zip_code>
</ai>
<ei>
<action>INSERT</action>
<id>26658</id>
<user_id>00860884</user_id>
<ji>
<action>INSERT</action>
<c_string10>5</c_string10>
<c_string11>P</c_string11>
<c_string12>MX_F_1E</c_string12>
</ji>
<ji>
<action>INSERT</action>
<c_string10>5</c_string10>
<c_string11>P</c_string11>
<c_string12>MX_F_1E</c_string12>
<c_string13>1310</c_string13>
<c_string16>90000269</c_string16>
<c_string17>Y</c_string17>
<c_string18>BCC</c_string18>
</ji>
<CI>
<action>INSERT</action>
<pg>M9</pg>
<pr>
<action>INSERT</action>
<pc>GL1003</pc>
<pc_type>AMOUNT</pc_type>
</pr>
</CI>
</ps>
</Emp>
</query>
Thanks.

Related

Python lxml.etree: how to add 'xml:lang="en-US"' as a namespace

I am trying to create a xml whose first element is:
<speak version="1.0"
xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="en-US">
</speak>
I am able to add the first attributes with...
from lxml.etree import Element, SubElement, QName, tostring
root = Element('speak', version="1.0",
xmlns="http://www.w3.org/2001/10/synthesis")
...but not the namespace xml:lang="en-US". Based on several tuto/question like this and this I tried many solutions but none worked.
For example, I tried this :
class XMLNamespaces:
xml = 'http://www.w3.org/2001/10/synthesis'
root.attrib[QName(XMLNamespaces.xml, 'lang')] = "en-US"
But the ouput is
<speak xmlns:ns0="http://www.w3.org/2001/10/synthesis" version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" ns0:lang="en-US">
How can I create the xml:lang="en-US" of my first xml element?
The special xml: prefix is associated with the http://www.w3.org/XML/1998/namespace URI.
The following code adds xml:lang="en-US" to the root element:
root.attrib[QName("http://www.w3.org/XML/1998/namespace", "lang")] = "en-US"

how to get child node value from a paren preferenced by another child value in Groovy readyAPI

I am trying to get values from a web service response in readyAPI, so i can pass it to another web service request, so i can create a automated test flow.
I have tried different code pieces most of them was a single line of code, which i prefer if it possible. I can take value from a node by typing the parent node by its attribute value. I also can get parent node by child nodes attribute value and use it to get another child value.
Here some examples:
First Format that I can use it to get childs value:
<webserviceResponse>
<documentslist>
<document #id="1">
<payment #currency="USD" >
<amount>1250.00</amount>
</payment>
</document>
<document #id="2">
<payment #currency="JPY" >
<amount>150.00</amount>
</payment>
</document>
<document #id="3">
<payment #currency="EUR" >
<amount>1170.00</amount>
</payment>
</document>
<!-- etc. -->
</documentslist>
-----> To get currency for a specific document
def webServiceResponse = "webservice#Response"
int index=2
def currency = context.expand('${'+webServiceResponse+'//*:document[#id="['+index+']"]//*:payment/#currency}')
-----> Result of this is "JPY"
<webserviceResponse>
<documentslist>
<document #id="1">
<payment #currency="USD" >
<amount>1250.00</amount>
</payment>
<refund>true</refund>
</document>
<document #id="2">
<payment #currency="JPY" >
<amount>150.00</amount>
</payment>
</document>
<document #id="3">
<payment #currency="EUR" >
<amount>1170.00</amount>
</payment>
<refund>false</refund>
</document>
<!-- etc. -->
</documentslist>
-------> To get a currency dependent on existence of a specific node
In this example we are looking the file from up to down and we are finding every refund nodes,
and taking currency value that is in the same block with the second time we see a refund node.
def webServiceResponse = "webservice#Response"
int index=2
def currrency= context.expand('${'+webServiceResponse+'(//*:refund)['+index+']//parent::*//*:payment/#currency}')
--------> Result for this is "EUR"
This one is that i cant take child value with the same way.
<webserviceResponse>
<documentslist>
<document>
<key>D_Computer</key>
<currency>USD</currency>
<amount>1250.00</amount>
<refund>true</refund>
</document>
<document>
<key>D_Keyboard</key>
<currency>JPY</currency>
<amount>150.00</amount>
</document>
<document>
<key>D_Monitor</key>
<currency>EUR</currency>
<amount>1170.00</amount>
<refund>false</refund>
</document>
<!-- etc. -->
</documentslist>
My problem with this one it doesn't have any attributes, has only values of the nodes. I know that it doesnt have an integer by the way but maybe i am doing wrong that i dont realize.
I want to get the amount value only dependent to the "key" nodes value which i am going to specify in the script.
result should show :150.00
Thank you for the very detailed and well written question.
You can use the below. Your problem is easy as there are no namespace in it.
Technique is same which you have dispalyed, its just that you need not to use # as its for attributes
def groovyUtils=new com.eviware.soapui.support.GroovyUtils(context)
def xml=groovyUtils.getXmlHolder("NameOfRequest#Response");
def currency=xml.getNodeValue("//*:documentslist/*:document[key='${key}']/*:amount");
log.info "Value of $key is " + currency
key="D_Monitor"
currency=xml.getNodeValue("//*:documentslist/*:document[key='${key}']/*:amount");
log.info "Value of $key is " + currency
Replace NameOfRequest with your Request's name
There is an alternative way too. I will post it as a separate answer so not to cause confusion. This one is still better than other one
There is an alternate way of doing things using Hashmap if the other answer is not working due to namespaces in your XML
Try this method
We are getting all values first by using getNodeValues and then since we have pair we are putting in hashmap.
Now you can retrieve anything.
def groovyUtils=new com.eviware.soapui.support.GroovyUtils(context)
def xml=groovyUtils.getXmlHolder("Request1#Response");
def keys=xml.getNodeValues("//*:documentslist/*:document/*:key")
def amounts=xml.getNodeValues("//*:documentslist/*:document/*:amount")
log.info keys.toString()
log.info amounts.toString()
HashMap h1=[:]
// Add the pair into hashmap and then retrieve
for(int i=0;i<keys.size();i++)
{
h1.put(keys[i],amounts[i])
}
def whichone="D_Computer"
log.info "Value for $whichone is " + h1.get(whichone)
Lets say you want to retrieve more than one value then you can use arrays.
i.e. take arrays as key,currency,amount,refund
so if you want to retrieve the refund for a key='Z' So using a for loop you can know that Z is present at 3 location in the array
then your refund should be refund[3]. Similarly currency[3] and amount[3]
Both the answers have their own relevance

How to get the Structure/Template id by Structure/Template name

I have a requirement that, Need to create JournalArticle with Structure and Template.While creating JournalArticle the method expecting the StructureId and TemplateId but these are generated by Liferay.So by name how can i get Id's of both.
Create and execute a DynamicQuery, like so (just replace Template with Structure to get structures):
DynamicQuery q = DynamicQueryFactoryUtil.forClass(DDMTemplate.class)
.add(PropertyFactoryUtil.forName("name").like("%YOUR NAME%"));
List<DDMTemplate> templates = DDMTemplateLocalServiceUtil.dynamicQuery(q);
You have to use like since the names of the structures/templates are saved like so:
<?xml version='1.0' encoding='UTF-8'?>
<root available-locales="de_DE" default-locale="de_DE">
<Name language-id="de_DE">YOUR NAME</Name>
</root>
There can be different names for different locales.
You can get StructureId (called DDMStructure) with this code
long classNameIdJournalArticle = ClassNameLocalServiceUtil.getClassNameId(JournalArticle.class);
DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(groupId, classNameIdJournalArticle, "myDDMStructureName");
And TemplateId (called DDMTemplate) with this code
DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(groupId, classNameIdDDMStructure, "ddmTemplateName");

Copy attribute values based on a preceding node attribute with Groovy

I'm trying to do something in SoapUi with Groovy and it's not working.
I have multiple nodes with many attributes and I need to extract the child node attribute based of a parent's attribute
For example:
<library id="82389389">
<book id="123" bookType="SF">
<price id="325" priceValue="5"/>
</book>
<book id="4741" bookType="History">
<price id="12388" priceValue="15"/>
</book>
<book id="2626" bookType="Drama">
<price id="12145" priceValue="40"/>
</book>
</library>
In this XML I need to extract priceValue based on the bookType and use it somewhere else (the order of the book nodes is changing)
I tried this but it doesn't work:
def response = .../library[1]
def i=0
def records = new XmlSlurper().parseText(response)
def size = records.book.size()
for (i=0,i<size,i++)
{
bookType1 = records.book[i].#bookType.first().value().text();
if (bookType1 == 'History')
{
def priceValueBook = records.book[i].price.#priceValue.first().value().text()
log.info priceValueBook
}
}
It's not clear exactly at what point you are trying to do this, but the following would work in SoapUI script step:
context.expand('${test_step#Response#//*:book[#bookType="History"]/*:price/#priceValue}')

How to get a child node value based on another child in the same group

I'm developing in C#, and am processing xml from an external soap call.
I have loaded the xml response into an XElement.
Given the following xml stub
<record>
<node>
<a>My title</a>
<name>title_en</name>
</node>
<node>
<a>...</a>
<name>contact_name</name>
</node>
.....
</record>
Using xpath in C#: I'm trying to do the follow when using the method XPathSelectElement.
where
\record\node\name == 'title_en' select \record\node\a
If there is a better method to use or another suggestion on how to preform the query, I'm open to ideas.
Thanks in advance.
You need a predicate to constrain which node elements you need:
/record/node[name = 'title_en']/a
You read this expression as "find the record element, find all its child elements named node that have a name child with value "title_en", and for each of those find all a children"
Use this:
var title = doc.Descendants("node")
.Where(x => (string)x.Element("name") == "title_en")
.Select(x => (string)x.Element("a"))
.FirstOrDefault();

Resources