Last active
June 3, 2016 18:31
-
-
Save max-dark/2c014ca0c8fb8fb4200b97e97f22892c to your computer and use it in GitHub Desktop.
pugixml test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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