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:

  1. Check if the current object is able to handle the current element
  2. Otherwise delegate to the parent (which may as well call the former parent)
  3. 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:

  1. Check if the current object is able to handle the current element
  2. Otherwise delegate to the parent
  3. 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.

[Caution]There is one pitfall:

1

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.

2

The destructor on the other hand must clean up the stack.


  ValueParser::ValueParser()
  {
    states.push(new ValueState(eNone)); 1
  }

  ValueParser::~ValueParser()
  {
    while (states.size() != 0)2
    {
      delete getTopValueState()->getValue();
      delete getTopValueState();
      states.pop();
    }
  }