Skip to content

Instantly share code, notes, and snippets.

@chaws
Created April 26, 2017 04:14
Show Gist options
  • Save chaws/86e6d1473b706ca8c55bd2dd7bf3683b to your computer and use it in GitHub Desktop.
Save chaws/86e6d1473b706ca8c55bd2dd7bf3683b to your computer and use it in GitHub Desktop.
// g++ btree.cpp
#include "rapidxml.hpp"
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
using namespace rapidxml;
xml_node<> * tree;
void buildTreeFromXml(char * filename)
{
xml_document<> doc;
// Read the xml file into a vector
ifstream theFile (filename);
vector<char> buffer((istreambuf_iterator<char>(theFile)), istreambuf_iterator<char>());
buffer.push_back('\0');
// Parse the buffer using the xml file parsing library into doc
doc.parse<0>(&buffer[0]);
// Find our root node
tree = doc.first_node();
tree->name();
cout << "first node is " << tree->name() << endl;
}
// Print node, go left and then right, recursively
void traverse(xml_node<> * node, string prefix)
{
prefix = prefix + "-" + node->name();
cout << prefix << ":";
// Visit following nodes
for (xml_node<> * child = node->first_node(); child; child = child->next_sibling())
{
traverse(child, prefix);
}
}
int main(int argc, char * argv[])
{
if(argc != 2)
{
printf("Usage %s file-to-process.xml\n", argv);
return -1;
}
buildTreeFromXml(argv[1]);
traverse(tree, ""), cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment