And now the main part. Parse consists of one large loop. Each iteration processes characters (if present) and then a tag. Processed text is removed from the beginning of text and when the text is empty, we’re done.

The only complication here is detecting empty elements and outputting <tag /> instead of <tag></tag>. To handle this, StartElement saves the tag in .element and then EndElement checks for it. The GetText method allows retrieving the text. For example:

A good improvement would be to “pretty print” the XML – adding newlines and indenting to make it more human readable.

These classes will be in stdlib in the next release. Until then, you can download sax.zip and use LibraryView > Import Records to load it into a library. This also includes simple XmlReaderTest and XmlWriterTest. Let me know what you think. I haven’t done much testing, so if you find any problems, please report them in the User Group.

Next, I’d like to write a simple version of XML-RPC (XML remote procedure calls). (Using these classes to process the XML, of course!)