<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<QAddress> <QHeader Revision="1.0" Mime="text/xml"> <Queue OperatorID="299" Name="Agent Addresses" Region="New England"/> <Comments Text="This is a sample list of addresses for mailing labels or envelopes."/> </QHeader> <QBody> <Address Row="1" Catalog="No"> <Contact>Joe Smith</Contact> <Name>Pedal Power Cycles</Name> <Urbanization>P.O. Box 1719</Urbanization> <Street>304 Hancock Street</Street> <City>Bangor</City> <State>ME</State> <Country>US</Country> <Zip>04402</Zip> </Address> . . . </QBody> </QAddress> |
/* This sample uses the SAX-reader, SAX-attributes and SAX-writer objects.
SAX-reader reads the XML document. The SAX parser passes the attributes of each new element to the StartElement callback procedure in a SAX-attributes object. The StartElement transforms the XML document by manipulating attributes and passes the altered SAX-attributes object to the SAX-writer object. The SAX-writer object writes the data out to a new XML document. */ DEFINE VARIABLE hSAXReader AS HANDLE NO-UNDO. DEFINE VARIABLE hSAXWriter AS HANDLE NO-UNDO. /* Keep the current element name in a global scope. */ DEFINE VARIABLE CurrentTag AS CHARACTER NO-UNDO. /* For a domestic mailing, do not use <Country> elements in new XML. */ DEFINE VARIABLE NoCountry AS LOGICAL NO-UNDO INITIAL TRUE. /* For customers with PO Boxes, do not use <Street> elements in new XML. */ DEFINE VARIABLE NoStreet AS LOGICAL NO-UNDO. /* New envelope line to request address correction. */ DEFINE VARIABLE EnvelopeSlug AS CHARACTER NO-UNDO INITIAL "Attention Post Master: Address Correction Requested.". CREATE SAX-WRITER hSAXWriter. hSAXWriter:FORMATTED = TRUE. hSAXWriter:STANDALONE = TRUE. hSAXWriter:ENCODING = "UTF-8". hSAXWriter:SET-OUTPUT-DESTINATION("FILE", "sax-readwrite.xml"). hSAXWriter:START-DOCUMENT( ). CREATE SAX-READER hSAXReader. hSAXReader:SET-INPUT-SOURCE("FILE", "sampledata.xml"). hSAXReader:SAX-PARSE( ). hSAXWriter:END-DOCUMENT( ). DELETE OBJECT hSAXWriter. DELETE OBJECT hSAXReader. /******************************************************************/ /* Callback procedures for SAX parser (SAX-reader object) */ /******************************************************************/ PROCEDURE StartElement: DEFINE INPUT PARAMETER namespaceURI AS CHARACTER NO-UNDO. DEFINE INPUT PARAMETER localName AS CHARACTER NO-UNDO. DEFINE INPUT PARAMETER qName AS CHARACTER NO-UNDO. DEFINE INPUT PARAMETER hSAXAttributes AS HANDLE NO-UNDO. ASSIGN CurrentTag = localName. IF localName = "Qheader" THEN hSAXAttributes:INSERT-ATTRIBUTE("MailDate", STRING(TODAY)). IF localName = "Address" THEN DO: hSAXAttributes:REMOVE-ATTRIBUTE("Row"). hSAXAttributes:UPDATE-ATTRIBUTE("Catalog", "Yes"). END. /* This address will use a PO Box instead of a Street Address */ IF localName = "Urbanization" THEN ASSIGN NoStreet = TRUE. /* Only call SAX-writer for elements wanted in new XML. */ CASE localName: WHEN "Country" THEN IF NOT NoCountry THEN hSAXWriter:START-ELEMENT(localName, namespaceURI, hSAXAttributes). WHEN "Street" THEN IF NOT NoStreet THEN hSAXWriter:START-ELEMENT(localName, namespaceURI, hSAXAttributes). OTHERWISE hSAXWriter:START-ELEMENT(localName, namespaceURI, hSAXAttributes). END CASE. END PROCEDURE. PROCEDURE Characters: DEFINE INPUT PARAMETER charData AS MEMPTR NO-UNDO. DEFINE INPUT PARAMETER numChars AS INTEGER NO-UNDO. DEFINE VARIABLE cData AS CHARACTER NO-UNDO. ASSIGN cData = GET-STRING(charData, 1, GET-SIZE(charData)). /* Only use 5 digit zip codes. */ IF CurrentTag = "Zip" THEN cData = SUBSTRING(cData, 1, 5). /* Only write content with SAX-writer if a start tag is waiting for content. "5" is a status of "SAX-WRITE-ELEMENT", which indicates that an end tag was last written and this current data is associated with a element not wanted in the output mailing list. */ IF hSAXWriter:WRITE-STATUS NE 5 THEN hSAXWriter:WRITE-CHARACTERS(cData). END PROCEDURE. PROCEDURE EndElement: DEFINE INPUT PARAMETER namespaceURI AS CHARACTER NO-UNDO. DEFINE INPUT PARAMETER localName AS CHARACTER NO-UNDO. DEFINE INPUT PARAMETER qName AS CHARACTER NO-UNDO. /* Only call SAX-writer for elements wanted in output mailing list. */ CASE localName: WHEN "Country" THEN IF NOT NoCountry THEN hSAXWriter:END-ELEMENT(localName, namespaceURI). WHEN "Street" THEN IF NOT NoStreet THEN hSAXWriter:END-ELEMENT(localName, namespaceURI). OTHERWISE hSAXWriter:END-ELEMENT(localName, namespaceURI). END CASE. /* Add another envelope line after Zip Code. */ IF localName = "Zip" THEN DO: hSAXWriter:START-ELEMENT("EnvelopeSlug", namespaceURI). hSAXWriter:WRITE-CHARACTERS(EnvelopeSlug). hSAXWriter:END-ELEMENT("EnvelopeSlug", namespaceURI). END. /* Reset check for PO Box versus Street address. */ IF localName = "Address" THEN ASSIGN NoStreet = FALSE. END PROCEDURE. |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<QAddress> <QHeader Revision="1.0" Mime="text/xml" MailDate="10/25/06"> <Queue OperatorID="299" Name="Agent Addresses" Region="New England"> </Queue> <Comments Text="Sample list of addresses..."> </Comments> </QHeader> <QBody> <Address Catalog="Yes"> <Contact>Joe Smith</Contact> <Name>Pedal Power Cycles</Name> <Urbanization>P.O. Box 1719</Urbanization> <City>Bangor</City> <State>ME</State> <Zip>04402</Zip> <EnvelopeSlug>Attention Post Master: Address Correction Requested. </EnvelopeSlug> </Address> . . . </QBody> </QAddress> |
/* This sample shows a programatically created SAX-attributes object used
to override the attributes of the input XML document. */ DEFINE VARIABLE hSAXWriter AS HANDLE NO-UNDO. DEFINE VARIABLE hSAXReader AS HANDLE NO-UNDO. DEFINE VARIABLE hMySAXAttributes AS HANDLE NO-UNDO. CREATE SAX-WRITER hSAXWriter. hSAXWriter:FORMATTED = TRUE. hSAXWriter:STANDALONE = TRUE. hSAXWriter:ENCODING = "UTF-8". hSAXWriter:SET-OUTPUT-DESTINATION("FILE", "sax-readwrite2.xml"). hSAXWriter:START-DOCUMENT( ). CREATE SAX-ATTRIBUTES hMySAXattributes. hMySAXAttributes:INSERT-ATTRIBUTE("MailDate", STRING(TODAY)). hMySAXAttributes:INSERT-ATTRIBUTE("Catalog", "Yes"). CREATE SAX-READER hSAXReader. hSAXReader:SET-INPUT-SOURCE("FILE", "sampledata.xml"). hSAXReader:SAX-PARSE( ). hSAXWriter:END-DOCUMENT( ). DELETE OBJECT hSAXWriter. DELETE OBJECT hSAXReader. DELETE OBJECT hMySAXAttributes. /* Callback procedures for SAX parser (SAX-reader object) */ PROCEDURE StartElement: DEFINE INPUT PARAMETER namespaceURI AS CHARACTER NO-UNDO. DEFINE INPUT PARAMETER localName AS CHARACTER NO-UNDO. DEFINE INPUT PARAMETER qName AS CHARACTER NO-UNDO. DEFINE INPUT PARAMETER hSAXAttributes AS HANDLE NO-UNDO. /* Only call SAX-writer with attributes for address elements. */ IF locaLNAME = "Address" THEN hSAXWriter:START-ELEMENT(localName, namespaceURI, hMySAXAttributes). ELSE hSAXWriter:START-ELEMENT(localName, namespaceURI). END. ... |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
. . . <Address MailDate="10/25/06" Catalog="Yes"> <Contact>Joe Smith</Contact> <Name>Pedal Power Cycles</Name> <Urbanization>P.O. Box 1719</Urbanization> <Street>304 Hancock Street</Street> <City>Bangor</City> <State>ME</State> <Country>US</Country> <Zip>04402</Zip> </Address> . . . </QBody> </QAddress> |