Fun with libraries

  • 25 April 2009

Parsing XML data is not a big problem. In theory. In practice you will most certainly use a framework or a library. I use C++ at the moment. And I use libxml++ which works really well. When walking through an XML document you just grab the content of the tags and get all the attributes you want. You have to be careful though. Accessing nonexistent attributes may lead to undefined behaviour. It’s best to stick to the example in the documentation and walk through the list of available attributes by iterator:

if ( (rule_type == USERAGENT) or (rule_type == REQUEST) ) {
    // Look for regexp attribute (still needs a try/catch structure ;)
    const xmlpp::Element::AttributeList& attributes = parentnode->get_attributes();
    for ( xmlpp::Element::AttributeList::const_iterator iter = attributes.begin();
          iter != attributes.end();
          ++iter) {
        const xmlpp::Attribute* attribute = *iter;
        if ( attribute->get_name() == "regexp" ) {
            string regexp_text = attribute->get_value();
            if ( regexp_text.length() > 0 ) {
                new_rule.pattern = make_u32regex(regexp_text);

Don’t access the regexp attribute directly, always get a list and look at the list of existing attributes. It saves a few core dumps. And yes, I am using the DOM parser. I have no idea what the SAX parser does. Maybe you want to try and tell me about it.

Sorry, the comment form is now closed.