tags:

views:

329

answers:

1

Can anyone share a snippet of code where they parsed a user defined object using SAX parser in C++.

+4  A: 

SAX stands for Simple API for XML.

SAX is for parsing XML files only. So if you want to parse an object from C++ using SAX, the only way this makes sense is to serialize that object into XML first.

The reason you would want to use a SAX XML parser though is because you may have a very large XML file, and you want to read it in parts only, by doing this you use less RAM.

A good exmaple of why you'd want to use a SAX parser is if you have a serizlied std::vector of strings into an XML file. Let's say this vector contains millions of entries. Then you can't read the entire XML file into memory at once. You would have to instead use a SAX parser.

There are many different implementations of a SAX parser, but you can even create one yourself pretty easily drawing a finite state machine. As you get into a state of reading a full element you call a callback function.

You can also use LibXML, MSXML, Xerces-C++ XML Parser, and many more libraries for SAX parsing.

Getting back to your original question. Your SAX parser would simply parse the XML representation of your object, then fill the object's members. Or if your object is a container, add the container's elements.

Brian R. Bondy