# libxml++ - An XML Parser for C++

Murray
Cumming

    murrayc@murrayc.com

12th September 2004

This is an introduction to libxml's C++ binding, with simple examples.

## libxml++

libxml++ is a C++ API for the popular libxml XML parser, written in C. libxml is famous for its high performance and compliance to standard specifications, but its C API is quite difficult even for common tasks.

libxml++ presents a simple C++-like API that can achieve common tasks with less code. Unlike some other C++ parsers, it does not try to avoid the advantages of standard C++ features such as namespaces, STL containers or runtime type identification, and it does not try to conform to standard API specifications meant for Java. Therefore libxml++ requires a fairly modern C++ compiler such as g++ 3.

But libxml++ was created mainly to fill the need for an API-stable and ABI-stable C++ XML parser which could be used as a shared library dependency by C++ applications that are distributed widely in binary form. That means that installed applications will not break when new versions of libxml++ are installed on a user's computer. Gradual improvement of the libxml++ API is still possible via non-breaking API additions, and new independent versions of the ABI that can be installed in parallel with older versions. These are the general techniques and principles followed by the GNOME project, of which libxml++ is a part.

### Installation

libxml++ is packaged by major Linux and *BSD distributions and can be installed from source on Linux and Windows, using any modern compiler, such as g++, SUN Forte, or MSVC++.

For instance, to install libxml++ and its documentation on debian, use apt-get or synaptic like so:

        # apt-get install libxml++2.6-dev libxml++2.6-doc

To check that you have the libxml++ development packages installed, and that your environment is working properly, try `pkg-config libxml++-2.6 --modversion`.

The source code may be downloaded from libxmlplusplus.sourceforge.net . libxml++ is licensed under the LGPL, which allows its use via dynamic linking in both open source and closed-source software. The underlying libxml library uses the even more generous MIT licence.

### UTF-8 and Glib::ustring

The libxml++ API takes, and gives, strings in the UTF-8 Unicode encoding, which can support all known languages and locales. This choice was made because, of the encodings that have this capability, UTF-8 is the most commonly accepted choice. UTF-8 is a multi-byte encoding, meaning that some characters use more than 1 byte. But for compatibility, old-fashioned 7-bit ASCII strings are unchanged when encoded as UTF-8, and UTF-8 strings do not contain null bytes which would cause old code to misjudge the number of bytes. For these reasons, you can store a UTF-8 string in a std::string object. However, the std::string API will operate on that string in terms of bytes, instead of characters.

Because Standard C++ has no string class that can fully handle UTF-8, libxml++ uses the Glib::ustring class from the glibmm library. Glib::ustring has almost exactly the same API as std::string, but methods such as length() and operator[] deal with whole UTF-8 characters rather than raw bytes.

There are implicit conversions between std::string and Glib::ustring, so you can use std::string wherever you see a Glib::ustring in the API, if you really don't care about any locale other than English. However, that is unlikely in today's connected world.

glibmm also provides useful API to convert between encodings and locales.

### Compilation and Linking

To use libxml++ in your application, you must tell the compiler where to find the include headers and where to find the libxml++ library. libxml++ provides a pkg-config .pc file to make this easy. For instance, the following command will provide the necessary compiler options: `pkg-config libxml++-2.6 --cflags --libs`

When using autoconf and automake, this is even easier with the PKG_CHECK_MODULES macro in your configure.ac file. For instance:

        PKG_CHECK_MODULES(SOMEAPP, libxml++-2.6 >= 2.10.0)
        AC_SUBST(SOMEAPP_CFLAGS)
        AC_SUBST(SOMEAPP_LIBS)

## Parsers

Like the underlying libxml library, libxml++ allows the use of 3 parsers, depending on your needs - the DOM, SAX, and TextReader parsers. The relative advantages and behaviour of these parsers will be explained here.

All of the parsers may parse XML documents directly from disk, a string, or a C++ std::istream. Although the libxml++ API uses only Glib::ustring, and therefore the UTF-8 encoding, libxml++ can parse documents in any encoding, converting to UTF-8 automatically. This conversion will not lose any information because UTF-8 can represent any locale.

Remember that white space is usually significant in XML documents, so the parsers might provide unexpected text nodes that contain only spaces and new lines. The parser does not know whether you care about these text nodes, but your application may choose to ignore them.

### DOM Parser

The DOM parser parses the whole document at once and stores the structure in memory, available via `Parser::get_document()`. With methods such as `Document::get_root_node()` and `Node::get_children()`, you may then navigate into the heirarchy of XML nodes without restriction, jumping forwards or backwards in the document based on the information that you encounter. Therefore the DOM parser uses a relatively large amount of memory.

You should use C++ RTTI (via `dynamic_cast<>`) to identify the specific node type and to perform actions which are not possible with all node types. For instance, only `Element`s have attributes. Here is the inheritance hierarchy of node types:

- xmlpp::Node:

  - xmlpp::Attribute

  - xmlpp::ContentNode

    - xmlpp::CdataNode

    - xmlpp::CommentNode

    - xmlpp::ProcessingInstructionNode

    - xmlpp::TextNode

  - xmlpp::Element

  - xmlpp::EntityReference

Although you may obtain pointers to the `Node`s, these `Node`s are always owned by their parent Nodes. In most cases that means that the Node will exist, and your pointer will be valid, as long as the `Document` instance exists.

There are also several methods which can create new child `Node`s. By using these, and one of the `Document::write_*()` methods, you can use libxml++ to build a new XML document.

#### Example

This example looks in the document for expected elements and then examines them. All these examples are included in the libxml++ source distribution.

Source Code

File: main.cc

    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif

    #include <libxml++/libxml++.h>

    #include <iostream>

    void print_indentation(unsigned int indentation)
    {
      for(unsigned int i = 0; i < indentation; ++i)
        std::cout << " ";
    }

    void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
    {
      std::cout << std::endl; //Separate nodes by an empty line.

      const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
      const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
      const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);

      if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
        return;

      const Glib::ustring nodename = node->get_name();

      if(!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text".
      {
        print_indentation(indentation);

        const Glib::ustring namespace_prefix = node->get_namespace_prefix();
        if(namespace_prefix.empty())
          std::cout << "Node name = " << nodename << std::endl;
        else
          std::cout << "Node name = " << namespace_prefix << ":" << nodename << std::endl;
      }
      else if(nodeText) //Let's say when it's text. - e.g. let's say what that white space is.
      {
        print_indentation(indentation);
        std::cout << "Text Node" << std::endl;
      }

      //Treat the various node types differently:
      if(nodeText)
      {
        print_indentation(indentation);
        std::cout << "text = \"" << nodeText->get_content() << "\"" << std::endl;
      }
      else if(nodeComment)
      {
        print_indentation(indentation);
        std::cout << "comment = " << nodeComment->get_content() << std::endl;
      }
      else if(nodeContent)
      {
        print_indentation(indentation);
        std::cout << "content = " << nodeContent->get_content() << std::endl;
      }
      else if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node))
      {
        //A normal Element node:

        //line() works only for ElementNodes.
        print_indentation(indentation);
        std::cout << "     line = " << node->get_line() << std::endl;

        //Print attributes:
        const xmlpp::Element::AttributeList& attributes = nodeElement->get_attributes();
        for(xmlpp::Element::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
        {
          const xmlpp::Attribute* attribute = *iter;
          print_indentation(indentation);

          const Glib::ustring namespace_prefix = attribute->get_namespace_prefix();
          if(namespace_prefix.empty())
            std::cout << "  Attribute " << attribute->get_name() << " = " << attribute->get_value() << std::endl;
          else
            std::cout << "  Attribute " << namespace_prefix  << ":" << attribute->get_name() << " = " << attribute->get_value() << std::endl;
        }

        const xmlpp::Attribute* attribute = nodeElement->get_attribute("title");
        if(attribute)
        {
          std::cout << "title found: =" << attribute->get_value() << std::endl;
        }
      }

      if(!nodeContent)
      {
        //Recurse through child nodes:
        xmlpp::Node::NodeList list = node->get_children();
        for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
        {
          print_node(*iter, indentation + 2); //recursive
        }
      }
    }

    int main(int argc, char* argv[])
    {
      // Set the global C++ locale to the user-configured locale,
      // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
      std::locale::global(std::locale(""));

      std::string filepath;
      if(argc > 1 )
        filepath = argv[1]; //Allow the user to specify a different XML file to parse.
      else
        filepath = "example.xml";

      #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
      try
      {
      #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
        xmlpp::DomParser parser;
        //parser.set_validate();
        parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically.
        parser.parse_file(filepath);
        if(parser)
        {
          //Walk the tree:
          const xmlpp::Node* pNode = parser.get_document()->get_root_node(); //deleted by DomParser.
          print_node(pNode);
        }
      #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
      }
      catch(const std::exception& ex)
      {
        std::cout << "Exception caught: " << ex.what() << std::endl;
      }
      #endif //LIBXMLCPP_EXCEPTIONS_ENABLED

      return 0;
    }

### SAX Parser

The SAX parser presents each node of the XML document in sequence. So when you process one node, you must have already stored information about any relevant previous nodes, and you have no information at that time about subsequent nodes. The SAX parser uses less memory than the DOM parser and it is a suitable abstraction for documents that can be processed sequentially rather than as a whole.

By using the `parse_chunk()` method instead of `parse()`, you can even parse parts of the XML document before you have received the whole document.

As shown in the example, you should derive your own class from SaxParser and override some of the virtual methods. These "handler" methods will be called while the document is parsed.

#### Example

This example shows how the handler methods are called during parsing.

Source Code

File: myparser.h

    #ifndef __LIBXMLPP_EXAMPLES_MYPARSER_H
    #define __LIBXMLPP_EXAMPLES_MYPARSER_H

    #include <libxml++/libxml++.h>

    class MySaxParser : public xmlpp::SaxParser
    {
    public:
      MySaxParser();
      virtual ~MySaxParser();

    protected:
      //overrides:
    #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
      virtual void on_start_document();
      virtual void on_end_document();
      virtual void on_start_element(const Glib::ustring& name,
                                    const AttributeList& properties);
      virtual void on_end_element(const Glib::ustring& name);
      virtual void on_characters(const Glib::ustring& characters);
      virtual void on_comment(const Glib::ustring& text);
      virtual void on_warning(const Glib::ustring& text);
      virtual void on_error(const Glib::ustring& text);
      virtual void on_fatal_error(const Glib::ustring& text);
    #endif
    };

    #endif //__LIBXMLPP_EXAMPLES_MYPARSER_H

File: main.cc

    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif

    #include <fstream>
    #include <iostream>

    #include "myparser.h"

    int
    main(int argc, char* argv[])
    {
      // Set the global C and C++ locale to the user-configured locale,
      // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
      std::locale::global(std::locale(""));

      std::string filepath;
      if(argc > 1 )
        filepath = argv[1]; //Allow the user to specify a different XML file to parse.
      else
        filepath = "example.xml";

      // Parse the entire document in one go:
      #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
      try
      {
      #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
        MySaxParser parser;
        parser.set_substitute_entities(true); //
        parser.parse_file(filepath);
      #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
      }
      catch(const xmlpp::exception& ex)
      {
        std::cout << "libxml++ exception: " << ex.what() << std::endl;
      }
      #endif //LIBXMLCPP_EXCEPTIONS_ENABLED

      // Demonstrate incremental parsing, sometimes useful for network connections:
      {
        //std::cout << "Incremental SAX Parser:" << std:endl;

        std::ifstream is(filepath.c_str());
        /* char buffer[64];
        const size_t buffer_size = sizeof(buffer) / sizeof(char); */

        //Parse the file:
        MySaxParser parser;
        parser.parse_file(filepath);

        //Or parse chunks (though this seems to have problems):
    /*
        do
        {
          memset(buffer, 0, buffer_size);
          is.read(buffer, buffer_size-1);
          if(is && is.gcount())
          {
            Glib::ustring input(buffer, is.gcount());
            parser.parse_chunk(input);
          }
        }
        while(is);

        parser.finish_chunk_parsing();
    */
      }

      return 0;
    }

File: myparser.cc

    #include "myparser.h"
    #include <glibmm/convert.h> //For Glib::ConvertError

    #include <iostream>

    MySaxParser::MySaxParser()
      : xmlpp::SaxParser()
    {
    }

    MySaxParser::~MySaxParser()
    {
    }

    #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
    void MySaxParser::on_start_document()
    {
      std::cout << "on_start_document()" << std::endl;
    }

    void MySaxParser::on_end_document()
    {
      std::cout << "on_end_document()" << std::endl;
    }

    void MySaxParser::on_start_element(const Glib::ustring& name,
                                       const AttributeList& attributes)
    {
      std::cout << "node name=" << name << std::endl;

      // Print attributes:
      for(xmlpp::SaxParser::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
      {
        try
        {
          std::cout << "  Attribute name=" << iter->name << std::endl;
        }
        catch(const Glib::ConvertError& ex)
        {
          std::cerr << "MySaxParser::on_start_element(): Exception caught while converting name for std::cout: " << ex.what() << std::endl;
        }

        try
        {
          std::cout << "    , value= " << iter->value << std::endl;
        }
        catch(const Glib::ConvertError& ex)
        {
          std::cerr << "MySaxParser::on_start_element(): Exception caught while converting value for std::cout: " << ex.what() << std::endl;
        }
      }
    }

    void MySaxParser::on_end_element(const Glib::ustring& /* name */)
    {
      std::cout << "on_end_element()" << std::endl;
    }

    void MySaxParser::on_characters(const Glib::ustring& text)
    {
      try
      {
        std::cout << "on_characters(): " << text << std::endl;
      }
      catch(const Glib::ConvertError& ex)
      {
        std::cerr << "MySaxParser::on_characters(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
      }
    }

    void MySaxParser::on_comment(const Glib::ustring& text)
    {
      try
      {
        std::cout << "on_comment(): " << text << std::endl;
      }
      catch(const Glib::ConvertError& ex)
      {
        std::cerr << "MySaxParser::on_comment(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
      }
    }

    void MySaxParser::on_warning(const Glib::ustring& text)
    {
      try
      {
        std::cout << "on_warning(): " << text << std::endl;
      }
      catch(const Glib::ConvertError& ex)
      {
        std::cerr << "MySaxParser::on_warning(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
      }
    }

    void MySaxParser::on_error(const Glib::ustring& text)
    {
      try
      {
        std::cout << "on_error(): " << text << std::endl;
      }
      catch(const Glib::ConvertError& ex)
      {
        std::cerr << "MySaxParser::on_error(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
      }
    }

    void MySaxParser::on_fatal_error(const Glib::ustring& text)
    {
      try
      {
        std::cout << "on_fatal_error(): " << text << std::endl;
      }
      catch(const Glib::ConvertError& ex)
      {
        std::cerr << "MySaxParser::on_characters(): Exception caught while converting value for std::cout: " << ex.what() << std::endl;
      }
    }
    #endif

### TextReader Parser

Like the SAX parser, the TextReader parser is suitable for sequential parsing, but instead of implementing handlers for specific parts of the document, it allows you to detect the current node type, process the node accordingly, and skip forward in the document as much as necessary. Unlike the DOM parser, you may not move backwards in the XML document. And unlike the SAX parser, you must not waste time processing nodes that do not interest you.

All methods are on the single parser instance, but their result depends on the current context. For instance, use `read()` to move to the next node, and `move_to_element()` to navigate to child nodes. These methods will return false when no more nodes are available. Then use methods such as `get_name()` and `get_value()` to examine the elements and their attributes.

#### Example

This example examines each node in turn, then moves to the next node.

Source Code

File: main.cc

    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif

    #include <libxml++/libxml++.h>
    #include <libxml++/parsers/textreader.h>

    #include <iostream>

    struct indent {
      int depth_;
      indent(int depth): depth_(depth) {};
    };

    std::ostream & operator<<(std::ostream & o, indent const & in)
    {
      for(int i = 0; i != in.depth_; ++i)
      {
        o << "  ";
      }
      return o;
    }

    int main(int /* argc */, char** /* argv */)
    {
      // Set the global C and C++ locale to the user-configured locale,
      // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
      std::locale::global(std::locale(""));

      #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
      try
      {
      #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
        xmlpp::TextReader reader("example.xml");

        while(reader.read())
        {
          int depth = reader.get_depth();
          std::cout << indent(depth) << "--- node ---" << std::endl;
          std::cout << indent(depth) << "name: " << reader.get_name() << std::endl;
          std::cout << indent(depth) << "depth: " << reader.get_depth() << std::endl;

          if(reader.has_attributes())
          {
            std::cout << indent(depth) << "attributes: " << std::endl;
            reader.move_to_first_attribute();
            do
            {
              std::cout << indent(depth) << "  " << reader.get_name() << ": " << reader.get_value() << std::endl;
            } while(reader.move_to_next_attribute());
            reader.move_to_element();
          }
          else
          {
            std::cout << indent(depth) << "no attributes" << std::endl;
          }

          if(reader.has_value())
            std::cout << indent(depth) << "value: '" << reader.get_value() << "'" << std::endl;
          else
            std::cout << indent(depth) << "novalue" << std::endl;

        }
      #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
      }
      catch(const std::exception& e)
      {
        std::cout << "Exception caught: " << e.what() << std::endl;
      }
      #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
    }