Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@max-dark
Last active June 3, 2016 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-dark/2c014ca0c8fb8fb4200b97e97f22892c to your computer and use it in GitHub Desktop.
Save max-dark/2c014ca0c8fb8fb4200b97e97f22892c to your computer and use it in GitHub Desktop.
pugixml test
/*
build: g++ -std=c++11 pugi_test.cpp -o ptest -lpugixml
Used docs:
- (XPath select)[http://pugixml.org/docs/manual.html#xpath.select]
- (XPath examples)[https://msdn.microsoft.com/ru-ru/library/ms256086(v=vs.120).aspx]
*/
#include <string>
#include <iostream>
#include <pugixml.hpp>
int main() {
// использую RAW строки из C++11
std::string xml_data;
xml_data = R"raw(<root>
<ent>
<target> text </target>
</ent>
<ent>
<fil>
<target> text </target>
</fil>
</ent>
</root>)raw";
pugi::xml_document xml;
// парсим XML из строки
auto res = xml.load_string(xml_data.c_str());
if (!res) {
// ошибка парсинга
std::cerr << "Fail: " << res.description() << std::endl;
return 1;
}
// получаем список всех нод с тегом <target>
auto nodes = xml.select_nodes("//target");
// проходимся по списку
for(auto& node : nodes) {
std::cout << node.node().text().as_string() << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment