Archive for the ‘XML’ Category

Using SoapUI to work with SOAP and WSDL request / responses

Tuesday, November 9th, 2010

My use of the software SoapUI has so far been minimal although I intend to further explore its capabilities, and a quick look at their website indicates that there are many. Currently I have been working with some Web Services Description Language (WSDL) documents utilizing SOAP for the service messaging. While true, a WSDL will contain all that one needs to know to invoke the web service, what if – you may ask, there was a program that could take that WSDL and create mock SOAP requests and responses based on the guidelines laid out in the WSDL.

Well, this is where the SoapUI comes into play (and as I previously stated it appears to do many other awesome things). A colleague of mine recommended using it, and it is certainly a program I will continue to utilize as it does create an easy to read set of mock requests and responses that significantly reduces the amount of time it takes to plan out a program utilizing a complex set of web services to perform.

Anyways you can read all about SoapUI on their website. Also, there is a free open source version for download at sourceforge. Enjoy!

Using an XPath wildcard

Tuesday, September 28th, 2010

In the case that you need to use XPath to retrieve an XML node, but are missing some piece of information you can use the ‘*’ as a wildcard in the XPath expression. Let’s take for example an application where a request for a certain type of house can be made and a set of XML representing that house and its details is returned. So let’s say the application requests a ‘Ranch House’. The XML returned looks like:

<House>
	<RanchHouse>
		<Floors>1</Floors>
		<Acres>8</Acres>
		<Basement>true</Basement>
	</RanchHouse>
</House>

And if we request a ‘Georgian House’ we would get:

<House>
	<GeorgianHouse>
		<Floors>3</Floors>
		<Acres>3</Acres>
		<Basement>false</Basement>
	</GeorgianHouse>
</House>

Notice that the child node of ‘House’ is different in both cases. While this may not be ideal XML it is valid and may be encountered. So we encounter this and we want to write a single XPath expression that will retrieve the number of floors for any house that is returned to us. In the expression where the the child node of ‘House’ is referenced we can use a wildcard to essentially bypass it and access its child nodes. If using the Coldfusion xmlSearch() function, our expression would look like:

<cfset variables.floors = xmlSearch(variables.myXmlObject, "/:House/*/:Floors") />

Use of the wildcard can go much further than this, but I do love how simple it is to skip over an xml node if its an unknown.

Using WDDX instead of JSON to pass string of numbers

Tuesday, March 2nd, 2010

I’m a big fan of working with JSON data, but I did run into a snag that annoyed me a little the other day. Simply put JSON takes it upon itself to convert a string of numbers to a data type number. Normally I wouldn’t notice, but in this case I was trying to return a zip code that began with a zero. Each time it was returned, the zero had been eliminated. I returned it as just a string, ran it through the tostring() method and so forth. All with no luck.

In the end I packaged the zip code in a wddx packet.  This sets the value into some xml which types it as a string if it is a string despite the fact that it is all numbers.  When the data is returned it is returned as a string, keeping the leading zero.  I’ve set up a quick demo to illustrate this issue.  If you open it in the firebug console you will be able to see the JSON that is returned in comparison to the XML in the WDDX packet.

Also remember that if you are returning structs or arrays in JSON format you can store a WDDX packet as a JSON value-pair within the struct or array.  I’ve quickly showed this below.  If there is a way to control the data typing of JSON data I’d love to know how.

<!--- SET ZIP CODE --->
<cfset var zipcode = "02906">

<!--- CREATE A STRUCT --->
<cfset var data = structnew()>
<cfset data.success = true>
<cfset data.message = "This is some type of message.">
<cfset data.zipcode = "">

<!--- SET ZIP INTO A WDDX PACKET IN THE STRUCT --->
<cfwddx action="cfml2wddx" input="#zipcode#" output="data.zipcode">