How to access XML node containing a period using groovy.util.XmlParser - groovy

I am needing to read a XML file where certain node names contain periods here is an example:
<build>
<actions>
<hudson.model.ParametersAction>
<safeParameters class="sorted-set"/>
<parameters>
<hudson.model.StringParameterValue>
...
When I try to use the standard build.actions.hudson.model.ParametersAction.parameters I get
groovy.lang.MissingPropertyException: Exception evaluating property 'hudson' for java.util.concurrent.CopyOnWriteArrayList, Reason: groovy.lang.MissingPropertyException: No such property: hudson for class: hudson.model.ParametersAction
I also tried build.actions.'hudson.model.ParametersAction'.parameters with this error:
groovy.lang.MissingPropertyException: Exception evaluating property 'hudson.model.ParametersAction' for java.util.concurrent.CopyOnWriteArrayList, Reason: groovy.lang.MissingPropertyException: No such property: hudson.model.ParametersAction for class: hudson.model.ParametersAction
How can I access this property?

You are right that it's necessary to surround a "periodish" element with quotes. The below code works for me. If it still doesn't work for you, it will be better if you share the code snippet which instantiates XmlParser and parses the xml.
def xml = """
<build>
<actions>
<hudson.model.ParametersAction>
<safeParameters class="sorted-set"/>
<parameters>
<hudson.model.StringParameterValue>foo</hudson.model.StringParameterValue>
</parameters>
</hudson.model.ParametersAction>
</actions>
</build>
"""
def parser = new XmlParser()
def build = parser.parseText(xml)
println(build.actions.'hudson.model.ParametersAction'.parameters.'hudson.model.StringParameterValue'.text())
//prints foo as a result

Related

In groovy Xpath function not working for a xml

I am having the below XML:
<orders>
<order xmlns:amz="http://www.amazon.co">
<amz:comp>amz</amz:comp>
</order>
<order xmlns:ebay="http://www.ebay.co">
<ebay:comp>ebay</ebay:comp>
</order>
</orders>
I checked the xpath expressions like //orders/order it is working but in groovy it is not working I'm not getting what is wrong.
Below is the code that I am using:
import groovy.xml.*;
def source = '''<orders>
<order xmlns:amz="http://www.amazon.co">
<amz:comp>amz</amz:comp>
</order>
<order xmlns:ebay="http://www.ebay.co">
<ebay:comp>ebay</ebay:comp>
</order>
</orders>'''
def root = new XmlSlurper().parseText(source).declareNamespace([
amz: "http://www.amazon.co",
ebay: "http://www.ebay.co"
])
println root.orders
println root.orders.order."amz:comp"
I am not getting any output for the above code.
In your example, root variable refers to the <orders> element, so when you call root.orders it is like you were looking for
<orders>
<orders>...</orders>
</orders>
Rename root variable to orders and do
println orders
println orders.order."amz:comp"
to get the following output:
amzebay
amz

'int-jdbc:stored-proc-outbound-gateway' is able to MAP ur Model element(s) by itself - is this a true statement?

Currently I am working with JDBC Spring Integration (to be very specific: <int-jdbc:stored-proc-outbound-gateway>) where the scenario is passing n-number of parameters to a stored procedure (ORACLE) and receive one return variable (with '1' or Error Message) along with n-number of CURSORS (sys_refcursor) which may have n-number of columns.
While working on this above scenario with Spring Framework I noticed the following issue(s):
My logic from front-end was, first read the 'return variable'. If that value is 1 then start reading all the CURSORS data or else through exception by redirecting user to the error page. Now, all my CURSORS were not having same number of columns as well as same length/type of data. And in middle layer I was having only one Model class to deal with all the CURSORS return elements. That was the challenge for me!
Previously I dealt with only one CURSOR. and therefore in middle layer I used Mapper class to map all CURSOR elements to my Model element's (getter and setter) to push data to the front. But then when used same concept to deal with n-number of CURSORS -that was truly a nightmare.
let me share what I did before and what I did later to resolve this situation at my end and then will draw my understanding for which will sick EXPARTS opinion at the later part of this post.
For one return variable (with '1' or Error Message) along with one CURSOR:
I had my Gateway definition as follows:
<!-- Stored Procedure Outbound-Gateway = To call a database stored procedure -->
<int-jdbc:stored-proc-outbound-gateway id="outbound-gateway-storedproc-personalinfo"
request-channel="procedureRequestChannel"
data-source="dataSource"
stored-procedure-name="pkg_personalinfo_spring.proc_personalinfo_spring"
expect-single-result="false"
ignore-column-meta-data="true">
<!-- Parameter Definitions -->
<int-jdbc:sql-parameter-definition name="firstname" direction="IN"/>
<int-jdbc:sql-parameter-definition name="lastname" direction="IN"/>
<int-jdbc:sql-parameter-definition name="p_RetVal" direction="OUT"/>
<int-jdbc:sql-parameter-definition name="get_ResultSet" type="#{T(oracle.jdbc.OracleTypes).CURSOR}" direction="OUT"/>
<!-- Parameter Mappings Before Passing & Receiving -->
<int-jdbc:parameter name="firstname" expression="payload[0]"/>
<int-jdbc:parameter name="lastname" expression="payload[1]"/>
<int-jdbc:returning-resultset name="get_ResultSet" row-mapper="com.support.PersonalinfoMapper"/>
</int-jdbc:stored-proc-outbound-gateway>
And in Mapper class I had the following simple Mapping rule:
...
PersonalInfo personalInfo = new PersonalInfo();
try{
personalInfo.setFirstname(resultSet.getString(DBConstants.FIRSTNAME));
personalInfo.setLastname(resultSet.getString(DBConstants.LASTNAME));
...
I did that because in my Model class i was having getter and setter for all CURSOR return elements.
Now, for one return variable (with '1' or Error Message) along with n-number of CURSORS:
I changed my Gateway definition as follows:
<!-- Stored Procedure Outbound-Gateway = To call a database stored procedure -->
<int-jdbc:stored-proc-outbound-gateway id="outbound-gateway-storedproc-personalinfo"
request-channel="procedureRequestChannel"
data-source="dataSource"
stored-procedure-name="pkg_personalinfo_spring.proc_personalinfo_spring"
expect-single-result="false"
ignore-column-meta-data="true">
<!-- Parameter Definitions -->
<int-jdbc:sql-parameter-definition name="firstname" direction="IN"/>
<int-jdbc:sql-parameter-definition name="lastname" direction="IN"/>
<int-jdbc:sql-parameter-definition name="p_RetVal" direction="OUT"/>
<int-jdbc:sql-parameter-definition name="get_curr_1" type="#{T(oracle.jdbc.OracleTypes).CURSOR}" direction="OUT"/>
<int-jdbc:sql-parameter-definition name="get_curr_2" type="#{T(oracle.jdbc.OracleTypes).CURSOR}" direction="OUT"/>
<!-- Parameter Mappings Before Passing & Receiving -->
<int-jdbc:parameter name="firstname" expression="payload[0]"/>
<int-jdbc:parameter name="lastname" expression="payload[1]"/>
</int-jdbc:stored-proc-outbound-gateway>
And secondly I removed the entire row mapping concept, means I didn't have any Mapper class. The only one thing I had was only one Model class with all the CURSORS element name and their getters and setters.
Also notice that in my 'Parameter Definitions', how added those two CURSORS definition but in 'Parameter Mappings Before Passing & Receiving' I have nothing now.
I ran the App without any exceptions and later through Fiddler observed the following JSON data came with RESPOND, means everything PERFECT! ;)
JSON
- {}
- get_curr_1
- -{}
- - firstname=Faisal
- - lastname=Quazi
- get_curr_2
- -{}
- - country=Bangladesh
- - capital=Dhaka
WHAT THE HACK??? Yes...I had the same feelings ;)
Now, can any one plz help to understand what is going on here. Will it be a true statement that "'int-jdbc:stored-proc-outbound-gateway' is able to MAP ur Model element(s) by itself magically???"
Is there any best practice to deal with such kind of situation(s)?
Thank u guys always :)
:-). I guess that by default Spring JDBC uses ColumnMapRowMapper.
So you end up after that gateway with Map<String, <Map<?, ?>>> or something similar. But it is Map anyway.
Since you further convert your payload to JSON and Jackson can get deal with maps prefectly, you finish with correct result.
I can debug it, of course, but deduction says that it can't be differently.
UPDATE
As I said. The answer is here JdbcTemplate#extractOutputParameters:
....
if (outParam.isResultSetSupported()) {
returnedResults.putAll(processResultSet((ResultSet) out, outParam));
}
else {
String rsName = outParam.getName();
SqlReturnResultSet rsParam = new SqlReturnResultSet(rsName, new ColumnMapRowMapper());
returnedResults.putAll(processResultSet((ResultSet) out, rsParam));
if (logger.isDebugEnabled()) {
logger.debug("Added default SqlReturnResultSet parameter named '" + rsName + "'");
}
}
....

Groovy MarkupBuilder

I have a list of things, each of which might be a foo or a bar. I want to build some xml that looks like this:
<rdf:RDF>
<foo id="1">
<foo id="2">
<bar id="3">
</rdf:RDF>
So I have gotten this far:
MarkupBuilder xml = new MarkupBuilder(writer)
xml.'rdf:RDF' (nsmap) { }
But now I am stuck. How do i - within that xml.'rdf:RDF' (nsmap) { } closure - iterate over my list of stuff? How do i - within that iterator - spit out a foo or a bar element as applicable?
Its simpler as you may think. Include a loop in the xml closure and in turn include markup in the loop. This script ...
import groovy.xml.MarkupBuilder
things = ['foo','foo','bar']
writer = new StringWriter()
xml = new MarkupBuilder(writer)
xml.'rdf:RDF' {
things.eachWithIndex {thing,index ->
"$thing" id:index+1
}
}
println writer
... will produce following output:
<rdf:RDF>
<foo id='1' />
<foo id='2' />
<bar id='3' />
</rdf:RDF>
Here You go:
import groovy.xml.MarkupBuilder
import org.custommonkey.xmlunit.*
def data = [foo:1,bar:2,baz:3]
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.'rdf:RDF' {
data.each { e ->
"$e.key"(id:e.value)
}
}
println writer
Ok.
The issue is that when a closure is run by a builder, it operates within the context of the builder. Now, it turns out that the builder context hides methods, but does not hide variables. This means that you can create a closure, assign it to a variable, and access it from within the builder.
NOw, I get the impression that this closure, too, runs in the context of the builder. But I suspect you can assign "this" to a variable outside the closures and then access that variable to get the context back.
It's ridiculous that you have to go to this sort of trouble. Im reminded of coding in Microsoft Access, back in the 90's. It works great, until the moment when you want to do something more than it was strictly designed to do.

Parsing an XML in Groovy through XML Slurper

I am having this strange problem of parsing an xml through XMLSlurper in groovy and it shows the size as 0. Cannot figure out why.
My xml file looks like:
<?xml version="1.0" encoding="iso-8859-1"?>
<sites>
<site name="OctUK">
<property name="warName">OctUKbuild-Deployable</property>
</site>
<site name="GbsJP">
<property name="warName">GbsJPbuild-Deployable</property>
</site>
</sites>
Code:
findSite("${project.GTA_BUILD_HOME}/platforms/pos/config/pos-sites.xml")
//Passed the path of the xml file to the method below:
GPathResult findSite(String sitesXml) {
xmlConfig = new XmlSlurper().parse(new File(sitesXml))
def siteGPath = xmlConfig.sites.site.findAll
// Check that a POS-sites.xml is valid
assert siteGPath.size() != 0, 'Error: no site found'
return(siteGPath)
}
The method fails with the error saying Error: no site found, because it is giving the result of siteGPath as 0. Not sure why it is giving the result as 0. It should have the size as 2.
Is there anything wrong I am doing. Any help is much appreciated. I am stuck at this point.
You don't need sites when looking at the xmlConfig object.
sites is the root node, so is implied, try:
assert xmlConfig.site.size() == 2
Also, xmlConfig.site is a instance of NodeChildren, but you seem to be declaring a return type of GPathResult
And I'm not sure what's missing from the end of your findAll call, as that should take a Closure, or empty parentheses

Parse XML using Groovy: Override charset in declaration and add XML processing instruction

My initial question have been answered, but that did just open up for further issues.
Example code
Using Groovy 2.0.5 JVM 1.6.0_31
import groovy.xml.*
import groovy.xml.dom.DOMCategory
def xml = '''<?xml version="1.0" encoding="UTF-16"?>
| <?xml-stylesheet type="text/xsl" href="Bp8DefaultView.xsl"?>
|<root>
| <Settings>
| <Setting name="CASEID_SEQUENCE_SIZE">
| <HandlerURL>
| <![CDATA[ admin/MainWindow.jsp ]]>
| </HandlerURL>
| </Setting>
| <Setting name="SOMETHING_ELSE">
| <HandlerURL>
| <![CDATA[ admin/MainWindow.jsp ]]>
| </HandlerURL>
| </Setting>
| </Settings>
|</root>'''.stripMargin()
def document = DOMBuilder.parse( new StringReader( xml ) )
def root = document.documentElement
// Edit: Added the line below
def pi = document.createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="Bp8DefaultView.xsl"');
// Edit #2: Added line below
document.insertBefore(pi, root)
use(DOMCategory) {
root.Settings.Setting.each {
if( it.'#name' == 'CASEID_SEQUENCE_SIZE' ) {
it[ '#value' ] = 100
}
}
}
def outputfile = new File( 'c:/temp/output.xml' )
XmlUtil.serialize( root , new PrintWriter(outputfile))
// Edit #2: Changed from root to document.documentElement to see if that
// would make any difference
println XmlUtil.serialize(document.documentElement)
Problem description
I'm trying to parse a XML-file exported from a third party tool, and before promoting it to stage and production I need to replace certain attribute values. That is all ok, but in addition I must keep the encoding and ref. to the stylesheet.
Since this is pretty static it is totally ok to have both the encoding and the stylesheet ref. definition in a property-file, meaning: I do not need first to find the declarations in the original file.
Encoding in declaration issue
As shown in this answer found here on StackOverFlow you can do
new File('c:/data/myutf8.xml').write(f,'utf-8')
I have also tried
XmlUtil.serialize( root , new GroovyPrintStream('c:/temp/output.txt', 'utf-16'))
but it did not solve the problem for me either. So I have not understood how to override the UTF-value.
Processing instruction issue
Simply, how do I add
<?xml-stylesheet type="text/xsl" href="Bp8DefaultView.xsl"?>
to the output?
Update - This can be done like this
def pi = document.createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="Bp8DefaultView.xsl"');
The processing instruction is being added like this, this guideline showed me, but still I do not get the output.
document.insertBefore(pi, root) // Fails
All issues in this question has been answered in another question I raised, see Groovy and XML: Not able to insert processing instruction
The trick is that I expected
document.documentElement
to contain the processing instruction. But that is wrong, documentElement is:
...This is a convenience attribute that allows direct access to the child node that is the document element of the document...
Where the processing instruction is just another child node. So what I instead had to use was either the LSSerializer or the Transfomer. See Serialize XML processing instruction before root element for details.

Resources