Friday 9 April 2021

Removing Namespaces from XML using JAVA Embedding



Hi,


Recently I came up with a requirement where I need to remove xmlns="" from below XML. So I did it using JAVA Embedding in BPEL.

Sample XML :

<?xml version="1.0" encoding="UTF-8"?>
<stores xmlns="http://www.demandware.com/xml/impex/store/2007-04-30">
<store store-id="R00591"  xmlns="">
<name>Irvine, CA</name>
<address1>YCC - IRVINE, CA #0591</address1>
<address2>735 SPECTRUM CENTER DRIVE</address2>
<city>IRVINE</city>
<postal-code>92618</postal-code>
<state-code>CA</state-code>
<country-code>US</country-code>
<phone>949-450-0725</phone>
<store-hours>Sunday 11:00 - 19:00|Monday 11:00 - 19:00|Tuesday 11:00 - 19:00|Wednesday 11:00 - 19:00|Thursday 11:00 - 19:00|Friday 11:00 - 20:00|Saturday 11:00 - 20:00</store-hours>
<latitude>33.6490286</latitude>
<longitude>-117.7449241</longitude>
<store-locator-enabled-flag>true</store-locator-enabled-flag>
<demandware-pos-enabled-flag>false</demandware-pos-enabled-flag>
<pos-enabled-flag>false</pos-enabled-flag>
<custom-attributes>
<custom-attribute attribute-id="ctaUrl">/stores/california/irvine-ca</custom-attribute>
</custom-attributes>
</store>
<store store-id="R00001"  xmlns="">
<name>Deerfield, MA</name>
<address1>YCC -SOUTH DEERFIELD, MA #0001</address1>
<address2>25 GREENFIELD ROAD</address2>
<city>SOUTH DEERFIELD</city>
<postal-code>01373</postal-code>
<state-code>MA</state-code>
<country-code>US</country-code>
<phone>413-665-2929</phone>
<store-hours>Sunday 10:00 - 18:00|Monday 10:00 - 18:00|Tuesday 10:00 - 18:00|Wednesday 10:00 - 18:00|Thursday 10:00 - 18:00|Friday 10:00 - 18:00|Saturday 10:00 - 18:00</store-hours>
<latitude>42.4744</latitude>
<longitude>-72.614073</longitude>
<store-locator-enabled-flag>true</store-locator-enabled-flag>
<demandware-pos-enabled-flag>false</demandware-pos-enabled-flag>
<pos-enabled-flag>false</pos-enabled-flag>
<custom-attributes>
<custom-attribute attribute-id="ctaUrl">/stores/massachusetts/south-deerfield-ma</custom-attribute>
</custom-attributes>
</store>
</stores>

We can remove  xmlns="" from payload using JAVA Embedding activity.ust follow below steps- 

Step 1. Assign "InvokeSFCCArchive_Write_InputVariable.body" Element type variable data as String using ora:getContentAsString() function to InputVar String type variable 

Example-

ora:getContentAsString($InvokeSFCCArchive_Write_InputVariable.body)   to $InputVar Type String


Step 2. Add below code to Java Embedding activity-

try{  

addAuditTrailEntry("JAVA activity Started");        

String inputXml = (String)getVariableData("InputVar");    

String input=inputXml.replace("xmlns=\"\"", "");

addAuditTrailEntry("String after Removing of xmlns  : "+input);      

setVariableData("tempXML",input.toString());  // Assigning processed xml to tempXML String Var

addAuditTrailEntry("inputtoString  : "+input.toString()); 

}   catch (Exception e) {           

addAuditTrailEntry("We got an Exception " + e.getMessage());           

}

Step 3. parseXML from tempXML to Element Type variable in Assign Activity

oraext:parseXML($tempXML)  to $InvokeSFCCArchive_Write_InputVariable.body


Processed XML :

<?xml version="1.0" encoding="UTF-8"?>
<stores xmlns="http://www.demandware.com/xml/impex/store/2007-04-30">
<store store-id="R00591">     <!-- No xmlns=""  -->
<name>Irvine, CA</name>
<address1>YCC - IRVINE, CA #0591</address1>
<address2>735 SPECTRUM CENTER DRIVE</address2>
<city>IRVINE</city>
<postal-code>92618</postal-code>
<state-code>CA</state-code>
<country-code>US</country-code>
<phone>949-450-0725</phone>
<store-hours>Sunday 11:00 - 19:00|Monday 11:00 - 19:00|Tuesday 11:00 - 19:00|Wednesday 11:00 - 19:00|Thursday 11:00 - 19:00|Friday 11:00 - 20:00|Saturday 11:00 - 20:00</store-hours>
<latitude>33.6490286</latitude>
<longitude>-117.7449241</longitude>
<store-locator-enabled-flag>true</store-locator-enabled-flag>
<demandware-pos-enabled-flag>false</demandware-pos-enabled-flag>
<pos-enabled-flag>false</pos-enabled-flag>
<custom-attributes>
<custom-attribute attribute-id="ctaUrl">/stores/california/irvine-ca</custom-attribute>
</custom-attributes>
</store>
<store store-id="R00001">  <!-- No xmlns=""  -->
<name>Deerfield, MA</name>
<address1>YCC -SOUTH DEERFIELD, MA #0001</address1>
<address2>25 GREENFIELD ROAD</address2>
<city>SOUTH DEERFIELD</city>
<postal-code>01373</postal-code>
<state-code>MA</state-code>
<country-code>US</country-code>
<phone>413-665-2929</phone>
<store-hours>Sunday 10:00 - 18:00|Monday 10:00 - 18:00|Tuesday 10:00 - 18:00|Wednesday 10:00 - 18:00|Thursday 10:00 - 18:00|Friday 10:00 - 18:00|Saturday 10:00 - 18:00</store-hours>
<latitude>42.4744</latitude>
<longitude>-72.614073</longitude>
<store-locator-enabled-flag>true</store-locator-enabled-flag>
<demandware-pos-enabled-flag>false</demandware-pos-enabled-flag>
<pos-enabled-flag>false</pos-enabled-flag>
<custom-attributes>
<custom-attribute attribute-id="ctaUrl">/stores/massachusetts/south-deerfield-ma</custom-attribute>
</custom-attributes>
</store>
</stores>


Your task is done!

Enjoy, Cheers !!


No comments:

Post a Comment

String to QR Code Image Generator Using Java

 Hi , Hope You are doing well. I came up with the requirement, where I need to generate QR code Image file for the input String. package dem...