Skip to content

jgsogo/xml_parser

Repository files navigation

XML Parser

Build status:

  • Unix (travis) Build Status
  • Visual Studio 2010 Build status
  • Biicode Build Status

Description

Tiny library to parse XML files using callbacks

Github repo (@jgsogo): https://github.com/jgsogo/xml_parser

Usage

Compile and install dependencies

At this moment only the EXPAT parser is implemented, so you will need to:

  1. install expat library first.
  2. compile and install this library, you can use CMake or Biicode (recommended, see below).

Create a parser

In order to work with the library the first thing you need is to create an instance of a parser. There are two possibilities to get an instance:

  1. Creating an object of the parser:

    #include "xml_parser/parser_expat.h"
    
    int main() {
        core::service::expat::parser_expat parser;
        // Play with the parser
    }
    
  2. Creating through the library interface

    #include "xml_parser/api.h"
     
    int main() {
        core::service::parser* parser;
        build_xml_parser(parser, "expat");        
        // Play with the parser        
        delete_xml_parser(parser);
    }
    

Parse a file

Create as many callback functions as needed and associate them with the xml nodes to parse:


// An example function that prints information about a node
auto log_node = [](const core::service::node& node) {
    std::cout << node.get_tag() << std::endl;
    auto attribs = node.get_attributes();
    for (auto it = attribs.begin(); it != attribs.end(); ++it) {
        std::cout << "\t" << it->first << ": " << it->second << std::endl;
    }
    if (node.has_data()) {
        std::cout << "\tDATA: " << node.get_data() << std::endl;
    }
    // Access childrent using node.get_childrent()
}

// Assign it to a given tag
parser.register_tag("xml_tag", log_node);

And let the parser run


parser.parse_file("filename.xml", "")

Usage with Biicode

To use it with Biicode just include the block


#include "jgsogo/xml_parser/xml_parser/parser_expat.h"

and let Biicode resolve the dependencies


bii find
bii cpp:build

Work ahead

This a concept library in development, there are several challenges ahead:

  • detach callback execution from file parsing

About

C++ XML parser

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published