I'm new to Mule ESB (3.6) and was testing out a simple flow to retrieve records from a MS SQL Server Express DB and convert results to XML using the 'Object to XML' Transformer.
The 'Customer' table only has 2 rows and my query is just returning all fields in the DB.
My problem: the XML returned is not what I expected (mapped based on a linked-list and not the XML structure based on the query). If I replace the transformer with a 'Object to JSON' version, the JSON returned represents the query results only (which is what I would expect).
I have followed a number of online tutorials and in all there is no mention of any extra settings/values to apply to the Object to XML transformer (so assuming the defaults apply).
Is this a bug or do I need to apply more settings somewhere?
XML of my Flow
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" basePath="/DBQuery" doc:name="HTTP Listener Configuration"/>
<db:generic-config name="Generic_Database_Configuration" url="jdbc:sqlserver://xxxx;databaseName=xxxx;user=xxxx;password=xxxx" doc:name="Generic Database Configuration" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<file:connector name="File" outputPattern="#[server.dateTime.format("yyyyMMdd'T'HHmmss.SSS")].xml" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/>
<flow name="dbqueryFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<db:select config-ref="Generic_Database_Configuration" doc:name="Database ">
<db:parameterized-query><![CDATA[SELECT CustomerNumber
,CustomerName
,CustomerAddressLine1
,CustomerAddressLine2
,CustomerSuburb
,CustomerStateName
,CustomerPostcode
FROM Customer]]>
</db:parameterized-query>
</db:select>
<mulexml:object-to-xml-transformer encoding="UTF-8" mimeType="text/xml" doc:name="Object to XML"/>
<file:outbound-endpoint path="C:\Mule\DBQuery\Backup" connector-ref="File" responseTimeout="10000" doc:name="File"/>
<logger level="INFO" doc:name="Logger" message="#[payload]"/>
</flow>
XML returned
<linked-list>
<org.mule.util.CaseInsensitiveHashMap serialization="custom">
<unserializable-parents/>
<org.mule.util.CaseInsensitiveHashMap>
<default/>
<float>0.75</float>
<int>16</int>
<int>7</int>
<string>CustomerNumber</string>
<string>1 </string>
<string>CustomerSuburb</string>
<string>Mt Eliza </string>
<string>CustomerStateName</string>
<string>Victoria </string>
<string>CustomerAddressLine1</string>
<string>Street name </string>
<string>CustomerPostcode</string>
<string>3930 </string>
<string>CustomerAddressLine2</string>
<string> </string>
<string>CustomerName</string>
<string>Sarge </string>
</org.mule.util.CaseInsensitiveHashMap>
</org.mule.util.CaseInsensitiveHashMap>
<org.mule.util.CaseInsensitiveHashMap serialization="custom">
<unserializable-parents/>
<org.mule.util.CaseInsensitiveHashMap>
<default/>
<float>0.75</float>
<int>16</int>
<int>7</int>
<string>CustomerNumber</string>
<string>2 </string>
<string>CustomerSuburb</string>
<string>Balwyn </string>
<string>CustomerStateName</string>
<string>Victoria </string>
<string>CustomerAddressLine1</string>
<string>Street name </string>
<string>CustomerPostcode</string>
<string>3920 </string>
<string>CustomerAddressLine2</string>
<string> </string>
<string>CustomerName</string>
<string>Daniel </string>
</org.mule.util.CaseInsensitiveHashMap>
</org.mule.util.CaseInsensitiveHashMap>
</linked-list>
JSON returned (DB fields only - which is what I expected)
[{"CustomerNumber":"1 ","CustomerSuburb":"Mt Eliza ","CustomerStateName":"Victoria ","CustomerAddressLine1":"Street name ","CustomerPostcode":"3930 ","CustomerAddressLine2":" ","CustomerName":"Sarge "},{"CustomerNumber":"2 ","CustomerSuburb":"Balwyn ","CustomerStateName":"Victoria ","CustomerAddressLine1":"Street name ","CustomerPostcode":"3920 ","CustomerAddressLine2":" ","CustomerName":"Daniel "}]
Also how do you remove the trailing white spaces from the output?
Thanks David
One option can be use custom transformers.
There are some ways to write custom transformer. In the code below I am using extending from
AbstractTransformer
which providedoTransform
method with 2 params. First thesource
and the second one is theencoding
. If you want to manipulate data from previous step in your flow usesrc
Custom Transformer:
Register custom transformer, in to order to make transformer available in your flows:
In your flow you have to refence to the custom transformer. You can see the sample below.
Flow:
Success!
After a lot of mucking around (and a crash course in learning XML with Java) and taking Eddu's post above, I was able to create a custom transformer.
Changed the 'Object to XML' transformer to a 'Java' transformer in my flow. Created Class SQLCustomerToXML (ensuring return type of String).
Transformer
Java Transformer settings
XML Result (in file as well as in browser)
David
Thanks David McInnes, you saved my day. A small improvement in code for handling Null exception in e.setTextContent().