Skip to content

Instantly share code, notes, and snippets.

@yudi-matsuzake
Created November 27, 2015 10:20
Show Gist options
  • Save yudi-matsuzake/7bbf586857ae4246be82 to your computer and use it in GitHub Desktop.
Save yudi-matsuzake/7bbf586857ae4246be82 to your computer and use it in GitHub Desktop.
Pequeno exemplo da utilização do RapidXML
#include <iostream>
#include <fstream>
#include <sstream>
#include <rapidxml/rapidxml.hpp>
#include <rapidxml/rapidxml_utils.hpp>
void bufferize(char* file_name, char*& buffer, size_t& size){
//tamanho
FILE* file = fopen(file_name, "r");
fseek(file, 0L, SEEK_END);
size = ftell(file);
fseek(file, 0L, SEEK_SET);
//lê do arquivo
buffer = new char[size + 1];
fread(buffer, sizeof(char), size, file);
buffer[size] = 0;
}
void foreach_node( rapidxml::xml_node<>* node, void (*func)(rapidxml::xml_node<>*, int), int indent=0 ){
if(node){
func(node, indent);
rapidxml::xml_node<>* i;
for(i=node->first_node(); i ; i = i->next_sibling()){
foreach_node( i, func, indent+1);
}
}
}
void print_attr(rapidxml::xml_node<>* node, int indent){
std::string tab;
for (int i=0; i< indent; i++){
tab += "\t";
}
std::cout << tab << "Oi eu sou o " << node->name() << std::endl;
std::cout << tab << "Se eu tenho valor? " << node->value() << std::endl;
rapidxml::xml_attribute<>* attr;
for(attr = node->first_attribute(); attr; attr = attr->next_attribute()){
std::cout << tab << attr->name() << ": " << attr->value() << std::endl;
}
}
int main(int argc, char* argv[]){
if(argc != 2) return 1;
rapidxml::file<> file(argv[1]);
//rapidxml
rapidxml::xml_document<> doc;
doc.parse<0>(file.data());
rapidxml::xml_node<>* root = doc.first_node();
foreach_node(root, print_attr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment