|
Boost Users : |
Subject: [Boost-users] Fast XML Parser
From: Jeff Dunlap (jeff_j_dunlap_at_[hidden])
Date: 2008-12-13 22:33:35
I've been looking at a bunch of C++ libraries for parsing XML and RapidXML
looks very interesting to me. They have some very impressive benchmarks on
their site due to it being a in-situ parser:
http://rapidxml.sourceforge.net/ (way faster than most other XML parsers).
I think that this is something that can be most useful for many of us. I
know that existing boost libs can get this done but probably more complex
than an exclusive lib for XML parsing.
Anyway, I cannot find samples anywhere and I am not experienced enough to
know how to use the lib. This is working code that I have so far (important
comments include):
#include "../test_utils.hpp"
#include "../../rapidxml.hpp"
#include "../../rapidxml_utils.hpp"
#include "../../rapidxml_print.hpp"
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>
#include <cstring>
#include <sstream>
using namespace std;
void test()
{
char xml[] = "<?xml version=\"1.0\" encoding=\"latin-1\"?>"
"<book>"
"</book>";
//Parse the original document
rapidxml::xml_document<> doc; // character type defaults to char
doc.parse<0>(xml); // 0 means default parse flags
// doc object is now a root of DOM tree containing representation of the
parsed XML.
// Because all RapidXml interface is contained inside namespace rapidxml,
users must either bring contents of this namespace into scope, or fully
qualify all the names.
// Class xml_document represents a root of the DOM hierarchy. By means of
public inheritance, it is also an xml_node and a memory_pool.
// Template parameter of xml_document::parse() function is used to specify
parsing flags, with which you can fine-tune behaviour of the parser.
// Note that flags must be a compile-time constant.
// To access the DOM tree, use methods of xml_node and xml_attribute
classes:
std::cout << "Name of my first node is: " << doc.first_node()->name() <<
"\n"; // Name of my first node is: book
// This works but is less descriptive than 'Create proper node type' below
//rapidxml::xml_node<> *node;
// Create proper node type
rapidxml::xml_node<char> *node = 0;
node = doc.allocate_node(/*rapidxml::node_declaration*/
rapidxml::node_element, "author", "John Doe");
doc.first_node()->append_node(node);
node = doc.allocate_node(rapidxml::node_element, "author", "Jane Doe");
doc.first_node()->append_node(node);
node = doc.allocate_node(rapidxml::node_element, "author", "Bob Doe");
doc.first_node()->append_node(node);
std::stringstream ss;
ss << *doc.first_node();
std::string result_xml = ss.str();
std::cout <<result_xml<<std::endl;
}
int main()
{
test();
return 0;
}
I just don't have the skill to figure out how to use this. If anyone can
provide some beginner snippets on how to use this library, it would be much
appreciated. Idiot proof snippets on how to add, edit, delete with comments
would be awsome.
Thank you
-- ELKNews FREE Edition - Empower your News Reader! http://www.atozedsoftware.com
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net