Using the Clarion SAX parser

May Clarion devs may not be aware that Clarion has had XML reading/writing capability since Clarion 6, when SoftVelocity shipped the CenterPoint C++ XML class library along with some wrapper Clarion classes. Unfortunately the open source CenterPoint XML project didn't last, and the original company web site is long gone. 

I published several articles on ClarionMag about the CenterPoint classes, including:

The CenterPoint classes include both SAX and DOM parsers. DOM parsers have to load the entire document into memory before you can use it, but having the entire document at your disposal can make it easier to read and manipulate data. SAX parsers fire callbacks for each element found, and are better for reading massive files where memory usage could be a problem. 

I experimented with the DOM parser. One thing I didn't ever try was reading an XML file with the SAX parser. But Unknown user did and he has kindly given me permission to post his example code. 

Here's the main program CLW:

                                        program
    include('cpxml.inc'),once
                                        map
                                        end
xmlFileName                             string(128)
mySAXParserClass                        class(SAXParserClass)
StartElement                                PROCEDURE(string name),derived
EndElement                                  PROCEDURE(string name),derived
Characters                                  PROCEDURE(string chars),derived
addID                                       byte(false)
addFirstName                                byte(false)
addLastName                                 byte(false)
addGender                                   byte(false)
                                        end
resultQueue                             queue,pre(RST)
id                                          long
firstName                                   cstring(21)
lastName                                    cstring(31)
gender                                      cstring(2)
                                        end
result                                  byte
count                                   long

Window                                  WINDOW('Result Queue after XML Parse'),AT(,,321,193),CENTER,SYSTEM,GRAY,AUTO
                                            LIST,AT(15,11,292,166),USE(?List1),HVSCROLL,VCR,FROM(resultQueue)
                                        END
    code
    xmlFileName = 'people_tags.xml'
    free(resultQueue)
    result = mySAXParserClass.ParseXMLFile(xmlFileName)
    if result <> 1
        message('Error during XML Parsing')
    else
        open(Window)
        accept
        end
        close(Window)
    end
mySAXParserClass.StartElement           PROCEDURE(string name)
    code
    case name
    of 'Table'
        clear(resultQueue)
        self.addID = false
        self.addFirstName = false
        self.addLastName = false
        self.addGender = false
    of 'id'
        self.addID = true
    of 'firstname'
        self.addFirstName = true
    of 'lastname'
        self.addLastName = true
    of 'gender'
        self.addGender = true
    end
mySAXParserClass.EndElement             PROCEDURE(string name)
    code
    case name
    of 'Table'
        add(resultQueue)
    of 'id'
        self.addID = false
    of 'firstname'
        self.addFirstName = false
    of 'lastname'
        self.addLastName = false
    of 'gender'
        self.addGender = false
    end

mySAXParserClass.Characters             PROCEDURE(string chars)
    code
    if self.addID = true
        RST:ID = chars
    end
    if self.addFirstName = true
        RST:FirstName = chars
    end
    if self.addLastName = true
        RST:LastName = chars
    end
    if self.addGender = true
        RST:Gender = chars
    end

And here's the output of the program:

Sometime before the CenterPoint project went offline, I downloaded the source code (see below). I don't know how this version of the source compares with the one shipped with Clarion. If you compile it, let me know how it goes. 

Downloads