This program can be called from the command line in one of the following ways:
Call ORDPARSE PARM('prod0_2.xml')
Call ORDPARSE PARM('/www/XML4RPG/trans/prod0_2.xml')
Program: ORDPARSE
//************************************************************************
// This program reads an xml file located in the IFS and writes new
// records to a PO Header and Detail file based on the xml content. It
// then sends a status messge indicating success or failure. The name of
// the xml file to be parsed, is passed into the program as an incoming
// PARM. In our example the file is located in the default folder, if it
// was in a different folder the incoming PARM would have to contain the
// entire path of the file.
// Example of call: Call ORDPARSE PARM('prod0_2.xml')
// call ORDPARSE PARM('/www/XML4RPG/trans/prod0_2.xml')
//************************************************************************
H dftactgrp(*no) bnddir('RXSBND')
FPOHeader if a e k disk prefix('H.') usropn
FPODetail if a e k DISK prefix('I.') usropn
D H e ds qualified extname(POHEADER)
D I e ds qualified extname(PODETAIL)
/copy rxs,RXSCp
D ORDPARSE pr
D ppStrmFile 30
D ORDPARSE pi
D ppStrmFile 30
D allHandler pr
D pType value like(RXS_Type)
D pXPath value like(RXS_XPath)
D pData value like(RXS_XmlData)
D pDataLen value like(RXS_Length)
D errHandler pr
D pCurLine 10i 0 value
D pCurCol 10i 0 value
D pErrStr 1024a value varying
D gError ds likeds(RXS_Error) inz
d gReqFile s like(RXS_FilePath)
d gHead01 c const('/OrderResponse/OrderResponse-
d Body/OrderResponseProperties')
d gHead02 c const('/OrderResponse/OrderResponse-
d Body/OrderResponsePartners')
d gDetail c const('/OrderResponse/OrderResponse-
d Body/OrderResponseDetails/-
d OrderResponseProductLineItem')
/free
exsr init;
monitor;
clear gError;
exsr parse;
if gError.code <> *blanks;
RXS_stdOutError('error': gError: *on);
return;
endif;
on-error;
RXS_stdOutError('error': RXS_catchError(): *on);
endmon;
*inlr = *on;
//-----------------------------------------------------------------------
// Initializations
//-----------------------------------------------------------------------
begsr init;
if not %open(poheader);
open poheader;
ENDIF;
if not %open(podetail);
open podetail;
ENDIF;
ENDSR;
//-----------------------------------------------------------------------
// Setup event handlers that will get called each time an element content
// or attribute is encountered. Then call the parser with the xml
// received in on the request.
//-----------------------------------------------------------------------
begsr parse;
// Here we are setting up the parser to notify us (call allHandler)
// whenever we encounter (1)The beginning of the xml stream (gHead01 + '>')
// so that we can clear out any existing PO Header program values (2)The
// end of the Header portion of the xml stream (gHead02 + '/>') so that we
// know we can safely create the new PO Header file record. (3)The beginning
// of the detail portion of the xml stream (gDetail + '>') so that we can
// clear any PO detail file fields before populating them with new values
// from the stream (4)The end of the detail portion of the xml stream
// (gDetail + '/>') so that we know we can write the new PO Detail file record.
RXS_addHandler(gHead01 + '>': %paddr(allHandler));
RXS_addHandler(gHead02 + '/>': %paddr(allHandler));
RXS_addHandler(gDetail + '>': %paddr(allHandler));
RXS_addHandler(gDetail + '/>': %paddr(allHandler));
RXS_allElemContentHandler(%paddr(allHandler));
RXS_allAttrHandler(%paddr(allHandler));
RXS_parse(ppStrmFile: RXS_STMF: %paddr(errHandler));
endsr;
/end-free
//------------------------------------------------------------------------
// This local sub procedure will be called for each element content and
// attribute event that occurs during the parsing of the PostAdr document.
// Based on the piece of data this sub procedure is being notified of it
// will place the value in the appropriate PF field.
//------------------------------------------------------------------------
P allHandler b
D allHandler pi
D pType value like(RXS_Type)
D pXPath value like(RXS_XPath)
D pData value like(RXS_XmlData)
D pDataLen value like(RXS_Length)
/free
select;
when pXPath = gHead01 + '>';
clear POHeaderr;
when pXPath = gHead01 + '/PurchaseOrderNumber/DocumentIdentifier/';
H.PONBR = RXS_charToNbr(pData: 0);
when pXPath = gHead01 + '/PurchaseOrderIssuedDate/DateTime/';
H.PODT = %date(pData:*ISO0);
when pXPath = gHead02 + '/Buyer/PartnerInformation/PartnerName/';
H.BUYER = pData;
when pXPath = gHead02 + '/Seller/PartnerInformation/PartnerName/';
H.SELLER = pData;
when pXPath = gHead02 + '/ShipTo/PartnerInformation/PartnerName/';
H.SHIPTO = pData;
when pXPath = gHead02 + '/Payer/PartnerInformation/PartnerName/';
H.PAYER = pData;
when pXPath = gDetail + '>';
clear PODETAILR;
I.PONBR = H.PONBR;
when pXPath = gDetail + '/LineNumber/';
I.POLINE = RXS_charToNbr(pData: 0);
when pXPath = gDetail + '/PurchaseOrderLineItemNumber/';
I.ITEMNBR = RXS_charToNbr(pData: 0);
when pXPath = gDetail + '/ProductIdentification/ProductName/';
I.ITMDESC = pData;
when pXPath = gDetail + '/ProductQuantity/Measurement/MeasurementValue/';
I.QTY = RXS_charToNbr(pData: 0);
when pXPath = gDetail +'/ProductQuantity/Measurement/UnitOfMeasureCode/';
I.UOM = pData;
when pXPath = gDetail + '/ScheduleDateTimeInformation/' +
'DateTimeInformation/DateTime@DateTimeQualifier';
I.SHPDATQ = pData;
when pXPath = gDetail + '/ScheduleDateTimeInformation/' +
'DateTimeInformation/DateTime/';
I.SHPDAT = %date(pdata:*ISO0);
when pXPath = gDetail + '/>';
write PODETAILR;
when pXPath = gHead02 + '/>';
write POHEADERR;
endsl;
/end-free
P e
//------------------------------------------------------------------------
// If an error occurs this local sub procedure will be called by the
// parser.
//------------------------------------------------------------------------
P errHandler b
D errHandler pi
D pCurLine 10i 0 value
D pCurCol 10i 0 value
D pErrStr 1024a value varying
/free
gError.code = 'ORDPARSE';
gError.severity = 100;
gError.pgm = 'ORDPARSE.errHandler';
gError.text =
'Line:' + %char(pCurLine) + ' Col:' + %char(pCurCol) + ' ' + pErrStr;
/end-free
P E
IFS File (input): www/myrxs/trans/prod0_2.xml
<?xml version="1.0"?>
<OrderResponse Version="2.0.2">
<Header>
<ThisDocumentIdentifier>
<DocumentIdentifier>20090313T193640Z</DocumentIdentifier>
</ThisDocumentIdentifier>
<ThisDocumentDateTime>
<DateTime DateTimeQualifier="On">20090313T193640Z</DateTime>
</ThisDocumentDateTime>
<RequestingDocumentIdentifier>
<DocumentIdentifier>76664</DocumentIdentifier>
</RequestingDocumentIdentifier>
<RequestingDocumentDateTime>
<DateTime DateTimeQualifier="On">20090313T193640Z</DateTime>
</RequestingDocumentDateTime>
<From>
<PartnerInformation>
<PartnerName>FMC Corporation</PartnerName>
<PartnerIdentifier Agency="D-U-N-S">001234560000</PartnerIdentifier>
<AddressInformation>
<AddressLine>1234 Market Street</AddressLine>
<CityName>San Francisco</CityName>
<StateOrProvince>CA</StateOrProvince>
<PostalCode>94101</PostalCode>
<PostalCountry>US</PostalCountry>
</AddressInformation>
</PartnerInformation>
</From>
<To>
<PartnerInformation>
<PartnerName>My Company</PartnerName>
<PartnerIdentifier Agency="D-U-N-S">0016549870000</PartnerIdentifier>
<AddressInformation>
<AddressLine>123 First St</AddressLine>
<CityName>St. Paul</CityName>
<StateOrProvince>MN</StateOrProvince>
<PostalCode>55101</PostalCode>
<PostalCountry>US</PostalCountry>
</AddressInformation>
</PartnerInformation>
</To>
</Header>
<OrderResponseBody>
<OrderResponseProperties>
<PurchaseOrderNumber>
<DocumentIdentifier>76664</DocumentIdentifier>
</PurchaseOrderNumber>
<PurchaseOrderIssuedDate>
<DateTime DateTimeQualifier="On">20090311</DateTime>
</PurchaseOrderIssuedDate>
<LanguageCode Domain="ISO 639-2/T">eng</LanguageCode>
<CurrencyCode Domain="ISO 4217">USD</CurrencyCode>
<ReferenceInformation ReferenceType="ShipmentIdentifier">
<DocumentReference>
<DocumentIdentifier>KN</DocumentIdentifier>
</DocumentReference>
</ReferenceInformation>
<SpecialInstructions InstructionType="PostingInstructions">ACCEPT</SpecialInstructions>
</OrderResponseProperties>
<OrderResponsePartners>
<Buyer>
<PartnerInformation>
<PartnerName>My Company</PartnerName>
<PartnerIdentifier Agency="D-U-N-S">0016549870000</PartnerIdentifier>
<ContactInformation>
<ContactName>John Smith</ContactName>
<EmailAddress></EmailAddress>
</ContactInformation>
<AddressInformation>
<AddressLine>658 55th Ave</AddressLine>
<CityName>Cleveland</CityName>
<StateOrProvince>MS</StateOrProvince>
<PostalCode>38732</PostalCode>
<PostalCountry>US</PostalCountry>
</AddressInformation>
</PartnerInformation>
</Buyer>
<Seller>
<PartnerInformation>
<PartnerName>Another Company</PartnerName>
<PartnerIdentifier Agency="D-U-N-S">0096325870000</PartnerIdentifier>
<AddressInformation>
<AddressLine>1735 Grove Street</AddressLine>
<CityName>San Francisco</CityName>
<StateOrProvince>CA</StateOrProvince>
<PostalCode>94101</PostalCode>
<PostalCountry>US</PostalCountry>
</AddressInformation>
</PartnerInformation>
</Seller>
<ShipTo>
<PartnerInformation>
<PartnerName>Even Another Company</PartnerName>
<PartnerIdentifier Agency="D-U-N-S">00842365970000</PartnerIdentifier>
<ContactInformation>
<TelephoneNumber>
<NationalPhoneNumber>555-222-1234</NationalPhoneNumber>
</TelephoneNumber>
</ContactInformation>
<AddressInformation>
<AddressLine>125 Wilshire Blvd</AddressLine>
<CityName>Los Angelos</CityName>
<StateOrProvince>CA</StateOrProvince>
<PostalCode>90001</PostalCode>
<PostalCountry>US</PostalCountry>
</AddressInformation>
</PartnerInformation>
</ShipTo>
<Payer>
<PartnerInformation>
<PartnerName>My Company</PartnerName>
<PartnerIdentifier Agency="D-U-N-S">0012345670000</PartnerIdentifier>
<ContactInformation>
<TelephoneNumber>
<NationalPhoneNumber>555-222-4321</NationalPhoneNumber>
</TelephoneNumber>
</ContactInformation>
<AddressInformation>
<AddressLine>123 Cleveland Ave</AddressLine>
<CityName>Mankato</CityName>
<StateOrProvince>MN</StateOrProvince>
<PostalCode>56001</PostalCode>
<PostalCountry>US</PostalCountry>
</AddressInformation>
</PartnerInformation>
</Payer>
</OrderResponsePartners>
<OrderResponseDetails>
<OrderResponseProductLineItem>
<LineNumber>1</LineNumber>
<PurchaseOrderLineItemNumber>1</PurchaseOrderLineItemNumber>
<ProductIdentification Agency="AssignedByManufacturer">
<ProductIdentifier>10000514</ProductIdentifier>
<ProductName>Fancy Widget</ProductName>
</ProductIdentification>
<SalesOrderIdentifier>
<DocumentReference>
<DocumentIdentifier>00123456</DocumentIdentifier>
<ReferenceItem>000023</ReferenceItem>
</DocumentReference>
</SalesOrderIdentifier>
<ProductQuantity>
<Measurement>
<MeasurementValue>2400</MeasurementValue>
<UnitOfMeasureCode Domain="UN/CEFACT Recommendation 20">GLL</UnitOfMeasureCode>
</Measurement>
</ProductQuantity>
<ScheduleDateTimeInformation ScheduleType="ConfirmDelivery">
<DateTimeInformation>
<DateTime DateTimeQualifier="OnOrBefore">20090406</DateTime>
</DateTimeInformation>
</ScheduleDateTimeInformation>
<ConfirmedPrice>
<Pricing PriceType="GrossPrice">
<PricingLumpSum>
<MonetaryAmount>
<MonetaryValue>123456.00</MonetaryValue>
<CurrencyCode Domain="ISO-4217">USD</CurrencyCode>
</MonetaryAmount>
</PricingLumpSum>
<PricingPerUnit>
<MonetaryAmount>
<MonetaryValue>125.12</MonetaryValue>
<CurrencyCode Domain="ISO-4217">USD</CurrencyCode>
</MonetaryAmount>
<PriceBasis>
<Measurement>
<MeasurementValue>2.5</MeasurementValue>
<UnitOfMeasureCode Domain="UN/CEFACT Recommendation 20">GLL</UnitOfMeasureCode>
</Measurement>
</PriceBasis>
</PricingPerUnit>
</Pricing>
</ConfirmedPrice>
</OrderResponseProductLineItem>
<OrderResponseProductLineItem>
<LineNumber>2</LineNumber>
<PurchaseOrderLineItemNumber>1</PurchaseOrderLineItemNumber>
<ProductIdentification Agency="AssignedByManufacturer">
<ProductIdentifier>00123456</ProductIdentifier>
<ProductName>Fancy Widget</ProductName>
</ProductIdentification>
<SalesOrderIdentifier>
<DocumentReference>
<DocumentIdentifier>0085361257</DocumentIdentifier>
<ReferenceItem>000003</ReferenceItem>
</DocumentReference>
</SalesOrderIdentifier>
<ProductQuantity>
<Measurement>
<MeasurementValue>2400</MeasurementValue>
<UnitOfMeasureCode Domain="UN/CEFACT Recommendation 20">GLL</UnitOfMeasureCode>
</Measurement>
</ProductQuantity>
<ScheduleDateTimeInformation ScheduleType="ConfirmDelivery">
<DateTimeInformation>
<DateTime DateTimeQualifier="OnOrBefore">20090317</DateTime>
</DateTimeInformation>
</ScheduleDateTimeInformation>
<ConfirmedPrice>
<Pricing PriceType="GrossPrice">
<PricingLumpSum>
<MonetaryAmount>
<MonetaryValue>123456.00</MonetaryValue>
<CurrencyCode Domain="ISO-4217">USD</CurrencyCode>
</MonetaryAmount>
</PricingLumpSum>
<PricingPerUnit>
<MonetaryAmount>
<MonetaryValue>125.12</MonetaryValue>
<CurrencyCode Domain="ISO-4217">USD</CurrencyCode>
</MonetaryAmount>
<PriceBasis>
<Measurement>
<MeasurementValue>2.5</MeasurementValue>
<UnitOfMeasureCode Domain="UN/CEFACT Recommendation 20">GLL</UnitOfMeasureCode>
</Measurement>
</PriceBasis>
</PricingPerUnit>
</Pricing>
</ConfirmedPrice>
</OrderResponseProductLineItem>
</OrderResponseDetails>
</OrderResponseBody>
</OrderResponse>
Physical File: POHEADER
*-------------------------------------------------------------------------
* @Name - POHEADER
* @Desc - Hold Purchase Order Header Information
*-------------------------------------------------------------------------
A UNIQUE
A R POHEADERR TEXT('PO Header')
A PONBR 7P 0 COLHDG('PO Number')
A PODT L COLHDG('Order Date')
A BUYER 25A COLHDG('Buyer Name')
A SELLER 25A COLHDG('Seller Name')
A SHIPTO 25A COLHDG('ShipTo Name')
A PAYER 25A COLHDG('Payer Name')
A K PONBR
Physical File: PODETAIL
*-------------------------------------------------------------------------
* @Name - PODETAIL
* @Desc - Hold Purchase Order Line Item Information
*-------------------------------------------------------------------------
A UNIQUE
A R PODETAILR TEXT('PO Line Item')
A PONBR 7P 0 COLHDG('PO Number')
A POLINE 3P 0 COLHDG('PO Line Number')
A ITEMNBR 9P 0 COLHDG('Item Number')
A ITMDESC 40A COLHDG('Item Description')
A QTY 7P 0 COLHDG('Item Quantity')
A UOM 3A COLHDG('Unit of Measure')
A SHPDATQ 25A COLHDG('Ship Date Qualifier')
A SHPDAT L COLHDG('Ship Date')
A K PONBR
A K POLINE