Setting up pugixml 1.15 in Visual Studio 2022 using CMake
- Install pugixml using this guide: How to install pugixml 1.15 using CMake and Visual Studio 2022 for Desktop
- Run Visual Studio
- Click on the "Create a new project" button:
- Click on the "CMake Project" and click on the "Next" button:
- Type a project name, set a project location, check the "Place solution and project in the same directory" checkbox, and click on the "Create" button:
- Wait a few seconds for configuration
- Run the project by click on the empty green triangle (or Ctrl+F5):
- The program prints "Hello CMake" to the console:
- Double click on the "CMakePresets.json" file:
- Add the "pugixml_DIR" variable for "Debug" and "Release":
| { |
| "name": "x64-debug", |
| "displayName": "x64 Debug", |
| "inherits": "windows-base", |
| "architecture": { |
| "value": "x64", |
| "strategy": "external" |
| }, |
| "cacheVariables": { |
| "CMAKE_BUILD_TYPE": "Debug", |
| "pugixml_DIR": { |
| "value": "E:/libs/pugixml-1.15-msvc/win/debug/lib/cmake/pugixml", |
| "type": "PATH" |
| } |
| } |
| }, |
| { |
| "name": "x64-release", |
| "displayName": "x64 Release", |
| "inherits": "x64-debug", |
| "cacheVariables": { |
| "CMAKE_BUILD_TYPE": "Release", |
| "pugixml_DIR": { |
| "value": "E:/libs/pugixml-1.15-msvc/win/release/lib/cmake/pugixml", |
| "type": "PATH" |
| } |
| } |
| }, |
Double click on the "CMakeLists.txt" file:
Add this code to the "CMakeLists.txt" file:
| find_package(pugixml) |
| target_link_libraries(hello-pugixml PRIVATE pugixml::pugixml) |
Create a simple XML file with the "users.xml" name inside of the "hello-pugixml/out/build/x64-debug" folder with the following content:
| <?xml version="1.0"?> |
| <people> |
| <person> |
| <name>Alice</name> |
| <age>30</age> |
| </person> |
| <person> |
| <name>Bob</name> |
| <age>25</age> |
| </person> |
| <person> |
| <name>Charlie</name> |
| <age>35</age> |
| </person> |
| <person> |
| <name>David</name> |
| <age>28</age> |
| </person> |
| <person> |
| <name>Eve</name> |
| <age>22</age> |
| </person> |
| </people> |
Replace the "hello-pugixml.cpp" file content with this one:
| #include <iostream> |
| #include <fstream> |
| #include <sstream> |
| #include <string> |
| #include <pugixml.hpp> |
| |
| std::string readFile(const std::string& path) |
| { |
| std::ifstream f(path); |
| if (!f.is_open()) |
| { |
| std::cout << "Failed to open the file: " << path << std::endl; |
| return ""; |
| } |
| std::stringstream sstr; |
| f >> sstr.rdbuf(); |
| std::string content = sstr.str(); |
| f.close(); |
| return content; |
| } |
| |
| int main() |
| { |
| |
| std::string fileContent = readFile("users.xml"); |
| |
| |
| pugi::xml_document doc; |
| |
| |
| pugi::xml_parse_result result = doc.load_string(fileContent.c_str()); |
| if (!result) |
| { |
| std::cerr << "Parse error: " << result.description() << std::endl; |
| return 1; |
| } |
| |
| |
| auto root = doc.first_child(); |
| auto people = root.children(); |
| for (pugi::xml_node person : people) |
| { |
| std::string name = person.child("name").text().as_string(); |
| int age = person.child("age").text().as_int(); |
| std::cout << name << ": " << age << std::endl; |
| } |
| |
| return 0; |
| } |
Press Ctrl+F5 to run the application
You see this result:
| Alice: 30 |
| Bob: 25 |
| Charlie: 35 |
| David: 28 |
| Eve: 22 |