Read file inside BPEL using Xpath function

To read a file in SOA composite, we can use the following options
    1. Using file adapter
    2. Using java embedding
    3. Using xpath function i.e. ora:readFile()

We generally use file adapter to read, write or poll files from different file locations. But, in case if don't want to create a file adapter and don't want to go for java embedding. We can make use of BPEL xpath extension functions. In this article, we will discuss about reading a file using xpath function ora:readFile().

Xpath expression:

    ora:readFile('fileName',['schemaLocation'],['rootElement'])    

xmlns:ora = http://schemas.oracle.com/xpath/extension

'fileName': Refers to absolute path of file along with extension. This is mandatory field. 

'schemaLocation' : Refers to location of nxsd schema for the file to be read. This is an optional field. If you don't provide the schema location, then data from the file will be read and converted to base64 encoded string

'rootElement' : Refers to root element in nxsd schema. This is optional field but required if we provide schema location.

Example:

Sample CSV file with absolute file path: C:\Users\test\SOA\test1.csv

S.No,Field1,Field2,Field3
1,t1,t2,t3
2,s1,s2,s3

Sample NXSD Schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:nxsd="http://xmlns.oracle.com/pcbpel/nxsd" xmlns:tns="http://TargetNamespace.com/ServiceName" targetNamespace="http://TargetNamespace.com/ServiceName" elementFormDefault="qualified" attributeFormDefault="unqualified" nxsd:version="NXSD" nxsd:stream="chars" nxsd:encoding="UTF-8" nxsd:hasHeader="true" nxsd:headerLines="1" nxsd:headerLinesTerminatedBy="${eol}">
  <xsd:element name="Root-Element">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="row" minOccurs="1" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="S.No" type="xsd:string" nxsd:style="terminated" nxsd:terminatedBy="," nxsd:quotedBy="&quot;"/>
              <xsd:element name="Field1" type="xsd:string" nxsd:style="terminated" nxsd:terminatedBy="," nxsd:quotedBy="&quot;"/>
              <xsd:element name="Field2" type="xsd:string" nxsd:style="terminated" nxsd:terminatedBy="," nxsd:quotedBy="&quot;"/>
              <xsd:element name="Field3" type="xsd:string" nxsd:style="terminated" nxsd:terminatedBy="${eol}" nxsd:quotedBy="&quot;"/>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>


Xpath statement without schema, which will give output as base64 schema.
    ora:readFile('file:///C:/Users/test/SOA/test1.csv')



Output:
    Uy5ObyxGaWVsZDEsRmllbGQyLEZpZWxkMw0KMSx0MSx0Mix0Mw0KMixzMSxzMixzMw0K

Xpath statement with schema, which will give output as xml
    ora:readFile('file:///C:/Users/test/SOA/test1.csv','Schemas/nxsd_schema1.xsd','Root-Element')



Output:
<Root-Element xmlns="http://TargetNamespace.com/ServiceName">
<row>
<S.No>1</S.No>
<Field1>t1</Field1>
<Field2>t2</Field2>
<Field3>t3</Field3>
</row>
<row>
<S.No>2</S.No>
<Field1>s1</Field1>
<Field2>s2</Field2>
<Field3>s3</Field3>
</row>
</Root-Element>

Using either of the above statements we can read file in assign activity directly, without using file adapter or java embedding.


Comments

Post a Comment

Popular posts from this blog

DateTime formatting using xp20:format-dateTime ()

Handling XML response with or without namespace in Postman

Create Delimited String from XML Nodes and Vice Versa in SOA 12c