xml toobjectesbtransform

13
XML to Object Esb

Upload: domenico-schiavone

Post on 18-Jan-2017

172 views

Category:

Engineering


0 download

TRANSCRIPT

XML to Object Esb

As we know, there can be multiple types of payload we need to deal daily, starting from JSON,  XML or String, we also require different type of data transformation in our application like XML to JSON, JSON to XML or String etc . In Mule application, we have a various set of transformers that we can use to obtain our required data format.

Today we will be discussing on transformation of XML to Object format of data. So, let us consider, we have the following XML payload as an input to our application

< user> <name>John</name> <lastName>Rich</lastName> </user>

Mule source<?xml version="1.0" encoding="UTF-8"?><mule xmlns:metadata=http://www.mulesoft.org/schema/mule/metadataxmlns:http=http://www.mulesoft.org/schema/mule/httpxmlns:http=http://www.mulesoft.org/schema/mule/httpxmlns="http://www.mulesoft.org/schema/mule/core"

xmlns:doc=http://www.mulesoft.org/schema/mule/documentationxmlns:spring=http://www.springframework.org/schema/beansxmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation="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/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsdhttp://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/><flow name="testweaveFlow"><http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/><dw:transform-message doc:name="Transform Message"><dw:input-payload doc:sample="empty.xml"/>

<dw:set-payload><![CDATA[%dw 1.0%type user = :object { class: "testdatajava.User" }%output application/java---{firstName: payload.user.name,lastName: payload.user.lastName

} as :user]]></dw:set-payload></dw:transform-message><byte-array-to-string-transformer doc:name="Byte Array to String"/><logger message="#[message]" level="INFO" doc:name="Logger"/></flow>

</mule>

In the transform code I created the Object type in the header“ %type user = :object { class: "testdatajava.User"} ”And the payload of the transform is converted to User Object using the transform logic as below.{firstName: payload.user.name,lastName: payload.user.lastName} as :user

Transformer

User.javapublic class User {

private String firstName;private String lastName;public String getFirstName() {return firstName;}

public void setFirstName(String firstName) {this.firstName = firstName;

}

public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}

@Overridepublic String toString() {// TODO Auto-generated method stubreturn this.firstName + " " + this.lastName;}}

Mule Flow