1.13. Using the XML Parser
ulxmlrpcpp uses expat
as parser
for XML. Additionally there are some helper classes. One of them forms a C++ wrapper to the
C-functions in expat
and one supports
parsing using a simple state machine. Nested XML elements
are modeled by deriving parser classes: "outer" elements derive from "inner"
elements.
Parsing of a start tag is done in the following steps:
- Check if the current object is able to handle the current element
- Otherwise delegate to the parent (which may as well call the former parent)
- Process the current element and remember the state for later use on a stack
Character data between the XML tags is stored for later use.
An ending tag is handled similarly:
- Check if the current object is able to handle the current element
- Otherwise delegate to the parent
- Check for a well formed document (current tag matches element on the stack).
Process stored character data.
For examples on the working method see the various *parse.cpp files.
| There is one pitfall: |
---|
| The first constructor in a chain of derived parsers must push an
appropriate element onto the state stack because the parser methods rely
upon being able to retrieve information about the current state. | | The destructor on the other hand must clean up
the stack. |
ValueParser::ValueParser()
{
states.push(new ValueState(eNone));
}
ValueParser::~ValueParser()
{
while (states.size() != 0)
{
delete getTopValueState()->getValue();
delete getTopValueState();
states.pop();
}
}
|