Tuesday, June 12, 2012

XML Parsing Script in IBM BPM

This script which is used to parse XML structure to value of a result during database calls. Similar script can be used to parse any XML structure and retrieve values  and store them in lombardi variables.

XML Structure :

Below XML is present in the variable "tw.local.result"

<resultSet recordCount="3" columnCount="6">
 <record> 

<column name="SERVICE_ID">3</column> <column name="SERVICE_NAME">service2</column> <column name="DESCRIPTION">dataentr</column> <column name="STATUS">1</column> <column name="PRODUCTID">4</column> <column name="PRODUCT_NAME">LOAN</column> </record>

<record> 
<column name="SERVICE_ID">2</column> <column name="SERVICE_NAME">service1</column> <column name="DESCRIPTION">data entry modify2</column> <column name="STATUS">1</column> <column name="PRODUCTID">3</column> <column name="PRODUCT_NAME">product1</column> </record> 

<record> 
<column name="SERVICE_ID">4</column> <column name="SERVICE_NAME">service3</column> <column name="DESCRIPTION">fkjsakghsakg</column> <column name="STATUS">0</column> <column name="PRODUCTID">3</column> <column name="PRODUCT_NAME">product1</column> </record> 

</resultSet>

Parsing script :

var resultLength=tw.local.result.childNodes.length;
log.error(tw.local.result.childNodes.length);
tw.local.serviceList=new tw.object.listOf.Service();
var i=0;
if(resultLength>0){
   
   
   while(resultLength>i){
   var firstElement=tw.local.result.childNodes.item(i);
   log.error(firstElement)
    tw.local.serviceList[i]=new tw.object.Service();
        tw.local.serviceList[i].serviceId=firstElement.xpath("record/column[@name='SERVICE_ID']")[0].getText(); 
        tw.local.serviceList[i].serviceName=firstElement.xpath("record/column[@name='SERVICE_NAME']")[0].getText();         
        tw.local.serviceList[i].description=firstElement.xpath("record/column[@name='DESCRIPTION']")[0].getText();         
        tw.local.serviceList[i].status=firstElement.xpath("record/column[@name='STATUS']")[0].getText();
        tw.local.serviceList[i].product=new tw.object.ProductType();
        tw.local.serviceList[i].product.productId=firstElement.xpath("record/column[@name='PRODUCTID']")[0].getText();
        tw.local.serviceList[i].product.productName=firstElement.xpath("record/column[@name='PRODUCT_NAME']")[0].getText();                                    
   
   i++;
   }

}

output variable:

The varaible "tw.local.serviceList" is a list of type "Service" has a final value as shown below:

<object type="Service[]"> <arrayElement size="3"> <item type="Service"> <property name="serviceName" type="String">service2</property> <property name="serviceId" type="Integer" tw-id="id:0">3</property> <property name="status" type="Integer" tw-id="id:0">1</property> <property name="description" type="String">dataentr</property> <property name="product" type="ProductType"> <property name="productName" type="String">LOAN</property> <property name="productId" type="Integer" tw-id="id:0">4</property> </property> </item> <item type="Service"> <property name="serviceName" type="String">service1</property> <property name="serviceId" type="Integer">2</property> <property name="status" type="Integer" tw-ref="id:0" /> <property name="description" type="String">data entry modify2</property> <property name="product" type="ProductType"> <property name="productName" type="String">product1</property> <property name="productId" type="Integer" tw-ref="id:0" /> </property> </item> <item type="Service"> <property name="serviceName" type="String">service3</property> <property name="serviceId" type="Integer" tw-ref="id:0" /> <property name="status" type="Integer">0</property> <property name="description" type="String">fkjsakghsakg</property> <property name="product" type="ProductType"> <property name="productName" type="String">product1</property> <property name="productId" type="Integer" tw-ref="id:0" /> </property> </item> </arrayElement> </object>



Below link illustrate on how to convert the xmlstring to xmlElement.
Link




1 comment:

  1. hey pradeep,

    I am having the XML in below format. Can you tell , how can i read the content from this XML ?





    date
    12/12/2009

    -
    branch
    Jordan


    accountno
    123456

    -



    ReplyDelete