Try OpenEdge Now
skip to main content
Working with XML
Writing XML Documents with the Simple API for XML (SAX) : Examples : Creating an XML document from database data
 

Creating an XML document from database data

The sw-example.p example outputs part of the Customer table of the Sports database.

sw-example.p

/* Write out the Customer table of the Sports2000 sample database using the
   methods of the SAX-WRITER Object */
DEFINE VARIABLE hSAXWriter AS HANDLE  NO-UNDO.
DEFINE VARIABLE lOK        AS LOGICAL NO-UNDO.

CREATE SAX-WRITER hSAXWriter.
hSAXWriter:FORMATTED = TRUE.
lOK = hSAXWriter:SET-OUTPUT-DESTINATION("file", "sw-example.xml").
lOK = hSAXWriter:START-DOCUMENT( ).
lOK = hSAXWriter:START-ELEMENT("customers").

FOR EACH Customer NO-LOCK WHERE Customer.CustNum < 5:
  ASSIGN
lOK = hSAXWriter:START-ELEMENT("customer")
lOK = hSAXWriter:INSERT-ATTRIBUTE("CustNum", STRING(Customer.CustNum))
lOK = hSAXWriter:INSERT-ATTRIBUTE("Name" , Customer.Name)
    lOK = hSAXWriter:WRITE-DATA-ELEMENT("Address", Customer.Address)
lOK = hSAXWriter:WRITE-DATA-ELEMENT("Address2", Customer.Address2)
lOK = hSAXWriter:WRITE-DATA-ELEMENT("City", Customer.City)
lOK = hSAXWriter:WRITE-DATA-ELEMENT("State", Customer.State)
lOK = hSAXWriter:WRITE-DATA-ELEMENT("PostalCode", Customer.PostalCode)
lOK = hSAXWriter:WRITE-DATA-ELEMENT("Country", Customer.Country)
lOK = hSAXWriter:WRITE-DATA-ELEMENT("Phone", Customer.Phone)
lOK = hSAXWriter:WRITE-DATA-ELEMENT("Contact", Customer.Contact)
lOK = hSAXWriter:WRITE-DATA-ELEMENT("SalesRep", Customer.SalesRep)
lOK = hSAXWriter:WRITE-DATA-ELEMENT("CreditLimit",
      STRING(Customer.CreditLimit))
lOK = hSAXWriter:WRITE-DATA-ELEMENT("Balance",
      STRING(Customer.Balance))
lOK = hSAXWriter:WRITE-DATA-ELEMENT("Terms", Customer.Terms)
lOK = hSAXWriter:WRITE-DATA-ELEMENT("Discount",
      STRING(Customer.Discount))
lOK = hSAXWriter:WRITE-DATA-ELEMENT("Comments", Customer.Comments)
    lOK = hSAXWriter:END-ELEMENT("customer").
END.

lOK = hSAXWriter:END-ELEMENT("customers").
lOK = hSAXWriter:END-DOCUMENT( ).

DELETE hSAXWriter.
Partial output from the sw-example.p program is shown below.

sw-example.xml (partial output)

<?xml version="1.0"?>
<customers>
<customer Cust-Num="1" Name="Lift Line Skiing">
<Address>276 North Street</Address>
<Address2></Address2>
<City>Boston</City>
<State>MA</State>
<Postal-Code>02114</Postal-Code>
<Country>USA</Country>
<Phone>(617) 450-0087</Phone>
<Contact>Gloria Shepley</Contact>
<Sales-Rep>HXM</Sales-Rep>
<Credit-Limit>66700</Credit-Limit>
<Balance>42568</Balance>
<Terms>Net30</Terms>
<Discount>35</Discount>
<Comments>This customer is on credit hold.</Comments>
</customer>
<customer Cust-Num="2" Name="Urpon Frisbee">
<Address>Rattipolku 3</Address>
<Address2></Address2>
<City>Valkeala</City>
<State>Uusimaa</State>
<Postal-Code>45360</Postal-Code>
<Country>Finland</Country>
<Phone>(60) 532 5471</Phone>
<Contact>Urpo Leppakoski</Contact>
<Sales-Rep>DKP</Sales-Rep>
<Credit-Limit>27600</Credit-Limit>
<Balance>17166</Balance>
<Terms>Net30</Terms>
<Discount>35</Discount>
<Comments>Ship all products 2nd Day Air.</Comments>
</customer>
. . .
</customers>