Let's say you have an XML input with some elements as below.
<SOAP-ENV:Body>
<Publish>
<Notification
message="<RegistraionNotificationEvent NotificationType="Place"><DeviceLocPlace
timestamp="1361210060855" accuracy="344.0"
macAddress="7b:29:a8:8b:e4:f8" lastSeen="7"
sourceTimestamp="1361209904211" x="171.34" y="104.513"/></RegistraionNotificationEvent>" />
</Publish>
</SOAP-ENV:Body>
You need to extract out the macAddress which is inside the message attribute. How can we do that?
If you closely look at this input, you would see, the macAddress stays inside the value of the attribute "message". But this message attribute carries another XML element which comes as a String.
Now what?
This is where WSO2 ESB Script mediator comes into rescue.
With just the following piece of configuration, you can extract out the Mac Address.
<script language="js">
<![CDATA[mc.setPayloadXML(new XML(mc.getPayloadXML()..*::Notification.@message.toXMLString()));]]></script>
The above segment converts the value of the attribute message into XML.
<property xmlns:ns="http://org.apache.synapse/xsd" name="mac" expression="//@macAddress" scope="default" type="STRING"/>
Using this segment, from the XML obtained above, you can extract out the macAddress part easily.
As handy as that...
<SOAP-ENV:Body>
<Publish>
<Notification
message="<RegistraionNotificationEvent NotificationType="Place"><DeviceLocPlace
timestamp="1361210060855" accuracy="344.0"
macAddress="7b:29:a8:8b:e4:f8" lastSeen="7"
sourceTimestamp="1361209904211" x="171.34" y="104.513"/></RegistraionNotificationEvent>" />
</Publish>
</SOAP-ENV:Body>
You need to extract out the macAddress which is inside the message attribute. How can we do that?
If you closely look at this input, you would see, the macAddress stays inside the value of the attribute "message". But this message attribute carries another XML element which comes as a String.
Now what?
This is where WSO2 ESB Script mediator comes into rescue.
With just the following piece of configuration, you can extract out the Mac Address.
<script language="js">
<![CDATA[mc.setPayloadXML(new XML(mc.getPayloadXML()..*::Notification.@message.toXMLString()));]]></script>
The above segment converts the value of the attribute message into XML.
<property xmlns:ns="http://org.apache.synapse/xsd" name="mac" expression="//@macAddress" scope="default" type="STRING"/>
Using this segment, from the XML obtained above, you can extract out the macAddress part easily.
As handy as that...
No comments:
Post a Comment