Search code examples
openedgeprogress-4gl

OpenEdge ABL - write XML


Is it possible to do this in Progress OpenEdge ABL? I already have the code for Company = A.

dataset dsOut:write-xml("FILE",dTempXmlFile,true,"UTF-8").

If Company = A then this is the xml

<?xml version="1.0" encoding="UTF-8"?>
  <LoadNotification>
    <LoadID>10167</LoadID>
    <ShipmentTransferNumber>2320373</ShipmentTransferNumber>
  </LoadNotification>

else if Company = B then this is the xml (notice an additional node appear)

<?xml version="1.0" encoding="UTF-8"?>
  <LoadNotification>
    <LoadID>10167</LoadID>
    <RetailID>AMZN</RetailID>
    <ShipmentTransferNumber>2320373</ShipmentTransferNumber>
  </LoadNotification>

I want to avoid self closing tag for company A.. I don't want to show <RetailID/> in company A

<?xml version="1.0" encoding="UTF-8"?>
  <LoadNotification>
    <LoadID>10167</LoadID>
    <RetailID/>
    <ShipmentTransferNumber>2320373</ShipmentTransferNumber>
  </LoadNotification>

Solution

  • The write-xml method has a ninth parameter omit-initial-values. If you can ensure that your RetailID matches the initial value (or reset it) when company is A then it will be skipped in the output.

    define temp-table tt
        field ii as int
        field cc as char initial ?
        .
    define dataset ds for tt.
    def var lcxml as longchar.
    
    create tt. assign tt.ii = 1 tt.cc = 'one'.
    create tt. assign tt.ii = 2. // no cc
    
    dataset ds:write-xml( 'longchar', lcxml, true, 'utf-8', ?, ?, ?, ?, true ).
    
    message string( lcxml ).
    

    Watch it run on ABL Dojo, otherwise output here:

    <?xml version="1.0" encoding="utf-8"?>
    <ds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <tt>
        <ii>1</ii>
        <cc>one</cc>
      </tt>
      <tt>
        <ii>2</ii>
      </tt>
    </ds>