Powered By Blogger

Wednesday, January 26, 2011

Removing unwanted namespace nodes from BPEL XML data

Use case- We have a requirement where we wanted to remove certain kinds of names space nodes from BPEL XML data and write it to file location.

Implementation steps:
  • Assume that we declare string variable- tmp in BPEL which holds the xml containing name space nodes  to be deleted
    
     
      
       
      

   


  • We also  declare another string variable - encStr in BPEL which holds transformed xml data
  • Java embedding activity with below code


     
try {       
addAuditTrailEntry("Try Block");    
String outputString = null;        
outputString = (java.lang.String)getVariableData("tmp");        
outputString = outputString.replaceAll("xmlns=\"http://xmlns.modeln.com/ApplicationObjects/MCOCheckAck/V1\"", "");            
outputString = outputString.replaceAll("xmlns:ns0=\"http://xmlns.modeln.com/ApplicationObjects/MCOCheckAck/V1\"", "");          
outputString = outputString.replaceAll("ns0:", "");
outputString = outputString.replaceAll(" xmlns=\"\"", "");     
addAuditTrailEntry("outputString"+outputString);     
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();     
DocumentBuilder db = dbf.newDocumentBuilder();     
org.w3c.dom.Document originalDoc = db.parse(new ByteArrayInputStream(outputString.getBytes()));     
         DOMSource domSource = new DOMSource(originalDoc);     
         StringWriter writer = new StringWriter();    
         addAuditTrailEntry("writer"+writer);    
         StreamResult result = new StreamResult(writer);    
         addAuditTrailEntry("result"+result);    
         TransformerFactory tf = TransformerFactory.newInstance();     
         Transformer transformer = tf.newTransformer();     
         transformer.transform(domSource, result);    
         addAuditTrailEntry("domSource"+domSource);    
         addAuditTrailEntry("result"+result);    
      
String encoded = Base64Encoder.encode(writer.toString());       
setVariableData("encStr",encoded);     
} catch(Exception ex) {       
ex.printStackTrace();       
}

Create a file adapter partner link with ‘Schema is Opaque ’ option




  • Next assign encStr variable to file invoke activity input variable
  • Add an invoke activity to invoke file adapter to write transformed xml
Disadvantages of this approach


We can not see transformed payload in audit trail of BPEL console because of binary format but if we check file write location you can see XML payload without name space nodes.

No comments:

Post a Comment