Build status:
Tiny library to parse XML files using callbacks
Github repo (@jgsogo): https://github.com/jgsogo/xml_parser
At this moment only the EXPAT parser is implemented, so you will need to:
- install expat library first.
- compile and install this library, you can use CMake or Biicode (recommended, see below).
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:
-
Creating an object of the parser:
#include "xml_parser/parser_expat.h" int main() { core::service::expat::parser_expat parser; // Play with the parser }
-
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); }
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", "")
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
This a concept library in development, there are several challenges ahead:
- detach callback execution from file parsing