Home > Enterprise >  Reading a .txt file in WSO2 Integration Studio
Reading a .txt file in WSO2 Integration Studio

Time:09-21

Is it possible to read a .txt file which has "|" as seperator using smooks? The file is similar to a normal csv file, only the extension is .txt. For csv we make use of "csv-reader", what can I use for reading a .txt file? Or is there any way to convert the .txt file into .csv in WSO2 Integration Studio?

This is the content of my file:

SYS_CD|PRO_CD|LOC_CD|AVA_QTY|RESQTY|PRO_QTY
POS|15|90|00|0|067656400

This is how I am calling the Smooks in my Proxy.

<smooks config-key="Smooks-config-file">
    <input type="text"/>
    <output type="xml"/>
</smooks>

This is my smooks-config-file

<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="Smooks-config-file" xmlns="http://ws.apache.org/ns/synapse">
    <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.5.xsd">
        <csv:reader fields="SYS_CD,PRO_CD,LOC_CD,AVA_QTY,RESQTY,PRO_QTY" separator="|" skipLines="1"/>
    </smooks-resource-list>
</localEntry>

CodePudding user response:

You can use the Smooks Mediator for this. You can simply read the file and use the property separator="|" to split the records by "|" and parse it to an XML.

<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.5.xsd">
    <csv:reader fields="name,age,$ignore$,address" separator="|" skipLines="1"/>
</smooks-resource-list>

Following is a VFS working sample.

Consumer Proxy

<?xml version="1.0" encoding="UTF-8"?>
<proxy name="Consumer" startOnLoad="true" transports="vfs" xmlns="http://ws.apache.org/ns/synapse">
    <target>
        <inSequence>
            <log description="Log student file records" level="full"/>
            <smooks config-key="smooks">
                <input type="text"/>
                <output type="xml"/>
            </smooks>
            <log description="Log student file records After===========" level="full"/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </target>
    <parameter name="transport.PollInterval">3</parameter>
    <parameter name="transport.vfs.ContentType">text/plain</parameter>
    <parameter name="transport.vfs.FileURI">file:///home/ycr/workspace/wso2/file/in</parameter>
    <parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter>
    <parameter name="transport.vfs.MoveAfterFailure">file:///home/ycr/workspace/wso2/file/out</parameter>
    <parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter>
    <parameter name="transport.vfs.FileNamePattern">.*\.txt</parameter>
    <parameter name="transport.vfs.MoveAfterProcess">file:///home/ycr/workspace/wso2/file/out</parameter>
</proxy>

Smooks Config

<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="smooks" xmlns="http://ws.apache.org/ns/synapse">
    <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.5.xsd">
        <csv:reader fields="SYS_CD,PRO_CD,LOC_CD,AVA_QTY,RESQTY,PRO_QTY" separator="|" skipLines="1"/>
    </smooks-resource-list>
</localEntry>

Output

[2022-09-20 08:24:24,200]  INFO {LogMediator} - {proxy:Consumer} To: , WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:1FE89F7256E5FF52E91663676664208, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><text xmlns="http://ws.apache.org/commons/ns/payload">SYS_CD|PRO_CD|LOC_CD|AVA_QTY|RESQTY|PRO_QTY
POS|15|90|00|0|067656400
</text></soapenv:Body></soapenv:Envelope>

[2022-09-20 08:24:24,204]  INFO {LogMediator} - {proxy:Consumer} To: , WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:1FE89F7256E5FF52E91663676664208, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><csv-set><csv-record number="1"><SYS_CD>POS</SYS_CD><PRO_CD>15</PRO_CD><LOC_CD>90</LOC_CD><AVA_QTY>00</AVA_QTY><RESQTY>0</RESQTY><PRO_QTY>067656400</PRO_QTY></csv-record></csv-set></soapenv:Body></soapenv:Envelope>
  • Related