Try OpenEdge Now
skip to main content
Working with XML
Writing XML Documents with the Simple API for XML (SAX) : Examples : Envelope information example
 

Envelope information example

Here is an example of a simple SAX-writer application that creates envelope information:
/* Program to write an envelope address */
DEFINE VARIABLE hSAXWriter AS HANDLE NO-UNDO.

CREATE SAX-WRITER hSAXWriter.
hSAXWriter:FORMATTED = TRUE. /* Format output so it is easy to read */
hSAXWriter:SET-OUTPUT-DESTINATION("file", "mailing.xml").

hSAXWriter:START-DOCUMENT( ).
/* The ENCODING attribute defaults to UTF-8 */
/* The FRAGMENT attribute defaults to FALSE */
/* The STRICT attribute defaults to TRUE */

hSAXWriter:START-ELEMENT("psc:mailingaddress").
hSAXWriter:DECLARE-NAMESPACE("www.progress.com", "psc").
RUN xmlData(INPUT "name", INPUT "John Smith").

hSAXWriter:START-ELEMENT("psc:address").
hSAXWriter:INSERT-ATTRIBUTE("type", "personal"). /* Node has an attribute */
RUN xmlData(INPUT "psc:street", INPUT "411 Whatsup St.").
RUN xmlData(INPUT "psc:city", INPUT "Somerville").
RUN xmlData(INPUT "psc:state", INPUT "MA").
RUN xmlData(INPUT "psc:zipcode", INPUT "02143").
hSAXWriter:END-ELEMENT("psc:address").

hSAXWriter:START-ELEMENT("psc:address").
hSAXWriter:INSERT-ATTRIBUTE("type", "business").
RUN xmlData(INPUT "psc:name", INPUT "Progress Software").
RUN xmlData(INPUT "psc:street", INPUT "14 Oak Park").
RUN xmlData(INPUT "psc:city", INPUT "Bedford").
RUN xmlData(INPUT "psc:state", INPUT "MA").
RUN xmlData(INPUT "psc:zip", INPUT "01730").
hSAXWriter:END-ELEMENT("psc:address").

hSAXWriter:WRITE-EMPTY-ELEMENT("psc:default").
hSAXWriter:INSERT-ATTRIBUTE("type", "personal").

hSAXWriter:END-ELEMENT("psc:mailingaddress").
hSAXWriter:END-DOCUMENT( ). /* Document written to working directory. */

PROCEDURE xmlData:
  DEFINE INPUT PARAMETER xmlNode  AS CHARACTER NO-UNDO.
  DEFINE INPUT PARAMETER charData AS CHARACTER NO-UNDO.

  hSAXWriter:START-ELEMENT(xmlNode).
  hSAXWriter:WRITE-CHARACTERS(charData).
  hSAXWriter:END-ELEMENT(xmlNode).
END PROCEDURE.
The code from the previous example produces a document like the following:
<?xml version="1.0">
<psc:mailingaddress xmlns:psc="www.progress.com">
    <psc:name>John Smith</psc:name>
    <psc:address type="personal">
        <psc:street>411 Whatsup St.</psc:street>
        <psc:city>Somerville</psc:city>
        <psc:state>MA</psc:state>
        <psc:zipcode>02143</psc:zipcode>
    </psc:address>
    <psc:name>John Smith</psc:name>
    <psc:address type="business">
        <psc:name>Progress Software</psc:name>
        <psc:street>14 Oak Park Drive</psc:street>
        <psc:city>Bedford</psc:city>
        <psc:state>MA</psc:state>
        <psc:zipcode>01730</psc:zipcode>
    </psc:address>
    <psc:default type="personal"/>
</psc:mailingaddress>
Instead of a procedure, you could also use the method for creating a leaf node. For example:
/* Program to write an envelope address */
DEFINE VARIABLE hSAXWriter AS HANDLE NO-UNDO
.
CREATE SAX-WRITER hSAXWriter.
hSAXWriter:FORMATTED = TRUE. /* Format output so it is easy to read */
hSAXWriter:SET-OUTPUT-DESTINATION("file", "mailing.xml").

hSAXWriter:START-DOCUMENT( ).
/* The ENCODING attribute defaults to UTF-8 */
/* The FRAGMENT attribute defaults to FALSE */
/* The STRICT attribute defaults to TRUE */

hSAXWriter:START-ELEMENT("psc:mailingaddress").
hSAXWriter:DECLARE-NAMESPACE("www.progress.com", "psc").
hSAXWriter:WRITE-DATA-ELEMENT("psc:name", "John Smith").

hSAXWriter:START-ELEMENT("psc:address").
hSAXWriter:INSERT-ATTRIBUTE("type", "personal"). /* Node has an attribute */
hSAXWriter:WRITE-DATA-ELEMENT("psc:street", "411 Whatsup St.").
hSAXWriter:WRITE-DATA-ELEMENT("psc:city", "Somerville").
hSAXWriter:WRITE-DATA-ELEMENT("psc:state", "MA").
hSAXWriter:WRITE-DATA-ELEMENT("psc:zipcode", "02143").
hSAXWriter:END-ELEMENT("psc:address").

hSAXWriter:START-ELEMENT("psc:address").
hSAXWriter:INSERT-ATTRIBUTE("type", "business"). /* Node has an attribute */
hSAXWriter:WRITE-DATA-ELEMENT("psc:name", "Progress Software").
hSAXWriter:WRITE-DATA-ELEMENT("psc:street", "14 Oak Park").
hSAXWriter:WRITE-DATA-ELEMENT("psc:city", "Bedford").
hSAXWriter:WRITE-DATA-ELEMENT("psc:state", "MA").
hSAXWriter:WRITE-DATA-ELEMENT("psc:zip", "01730").
hSAXWriter:END-ELEMENT("psc:address").

hSAXWriter:WRITE-EMPTY-ELEMENT("psc:default").
hSAXWriter:INSERT-ATTRIBUTE("type", "personal").

hSAXWriter:END-ELEMENT("psc:mailingaddress").
hSAXWriter:END-DOCUMENT( ). /* Document written to working directory. */