philip
philip

Reputation: 504

Using XML Transformer in Mule

I currently receive a string message from a queue, The string message contains XML. How do I pick each element and convert to a POJO. This is the content of my queue

<MsgContent>
   <MsgHeader>
       <code>1010</code>
   </MsgHeader>
   <MsgBody>
        <value>fundstransfer</value>
   </MsgBody>
</MsgContent>

I want to be able to get the values of the two keys(code and value)

This is my mule configuration xml

    <?xml version="1.0" encoding="UTF-8"?>

    <mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:data-        mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper"          xmlns:xm="http://www.mulesoft.org/schema/mule/xml"  xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:file="http://www.mulesoft.org/schema/mule/file"   xmlns:jms="http://www.mulesoft.org/schema/mule/jms" 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.4.0" 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/xml  http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
    http://www.mulesoft.org/schema/mule/core  http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
    http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
   <jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>

<mulexml:jaxb-context name="myJaxb" doc:name="myJaxb" packageNames="com.test.jaxb" />
<flow name="JMSMessageFlow1" doc:name="JMSMessageFlow1">
    <jms:inbound-endpoint queue="StudioIns" connector-ref="Active_MQ" doc:name="JMS" />
    <custom-transformer class="org.mule.module.xml.transformer.XmlToXMLStreamReader" />
    <mulexml:jaxb-xml-to-object-transformer  jaxbContext-ref="myJaxb" returnClass="com.test.jaxb.MsgContent"/>
    <component class="com.test.HelloMessage" doc:name="Java"/>
    <file:outbound-endpoint path="D:\Documents\MuleStudio\workspace\jmsmessage\src\main\java\com\test" outputPattern="output.csv" responseTimeout="10000" doc:name="File"/>
</flow>

But still can't retrieve the objects

Upvotes: 1

Views: 5249

Answers (3)

philip
philip

Reputation: 504

I have resolved it now. The major issue is that one must have a valid and correct JAXB Binding Class. Thanks all

Upvotes: 0

jonfornari
jonfornari

Reputation: 520

In my app i have a xml declaring all the transformers i need to use so that i just reference them on my flows. See below. (You dont need to use iso-8859-1). This one considers you intend to use JAXB to bind the XML to POJO.

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mxml="http://www.mulesoft.org/schema/mule/xml"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:spring="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/3.2/mule-xml.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.1/mule-vm.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<mxml:jaxb-context
name="jaxb-context"
packageNames="my.package.v1:my.package.v2:soap.package" />

<mxml:jaxb-xml-to-object-transformer
name="xml-to-object"
encoding="ISO-8859-1"
jaxbContext-ref="jaxb-context"
ignoreBadInput="true" />

<mxml:jaxb-object-to-xml-transformer
name="object-to-xml"
encoding="ISO-8859-1"
jaxbContext-ref="jaxb-context"
returnClass="java.lang.String"
ignoreBadInput="true" />

<byte-array-to-string-transformer
name="byte-array-to-string"
encoding="ISO-8859-1"
returnClass="java.lang.String" />

<string-to-byte-array-transformer
name="string-to-byte-array"
encoding="ISO-8859-1"
returnClass="byte[]" />

<custom-transformer
name="xml-to-xml-stream-reader"
class="org.mule.module.xml.transformer.XmlToXMLStreamReader" />

<object-to-string-transformer name="object-to-string" />

<flow name="ByteArrayToObjectXml">

<vm:inbound-endpoint
    path="myapp/conversor/byte-array-to-object-xml"
    exchange-pattern="request-response" />

<transformer ref="byte-array-to-string" />
<transformer ref="xml-to-object" />
</flow>

Upvotes: 0

David Dossot
David Dossot

Reputation: 33413

You can't use the xml namespace for this, it's reserved. People typically use xm or mulexml.

For example:

xmlns:xm="http://www.mulesoft.org/schema/mule/xml"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd"

...

<xm:xml-to-object-transformer />

Upvotes: 3

Related Questions