最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++工具庫之PugiXML使用實戰(zhàn)指南

 更新時間:2026年03月16日 10:45:49   作者:tianzhiyi1989sq  
pugixml是一個高效、易用的C++ XML處理庫,適用于各種項目,如游戲引擎和工具鏈,項目熱度高,依賴廣泛,使用MIT許可證,通過學習示例,可以掌握遍歷、查詢和修改XML文檔的方法,感興趣的朋友跟隨小編一起看看吧

pugixml是一個非常經(jīng)典且優(yōu)秀的C++ XML處理庫,很適合用來學習項目結(jié)構(gòu)和高效代碼。針對你提到的v1.15版本,我整理了它的詳細信息和使用步驟。

?? 項目熱度與基礎(chǔ)信息

根據(jù)開源數(shù)據(jù)平臺的信息,pugixml 是一個高知名度的C++項目。

  • Star 數(shù)量約 3,345 (數(shù)據(jù)隨時間略有浮動)。
  • 熱度分析:擁有超過 330個 公共代碼庫依賴,以及 12個 軟件包引用,在vcpkg包管理器的熱度排名中位于前5.4%。這些數(shù)據(jù)表明,它是一個被廣泛認可和使用的穩(wěn)定庫。
  • 開源協(xié)議:使用 MIT 許可證。這是一個非常寬松的協(xié)議,允許你在商業(yè)項目中自由使用、修改和分發(fā),只需保留原作者的版權(quán)聲明即可。
MIT 許可證是一種非常寬松的“允許型”(Permissive)開源許可證,它明確允許您將使用該協(xié)議的軟件應(yīng)用于商業(yè)項目 。
核心商業(yè)權(quán)利
具體來說,在 MIT 許可證下,您擁有以下權(quán)利 :
商業(yè)使用:您可以將 PugiXML 庫集成到您的商業(yè)產(chǎn)品中并出售。
修改:您可以根據(jù)需要修改 PugiXML 的源代碼。
分發(fā):您可以分發(fā)包含 PugiXML 的軟件,無論是源代碼形式還是目標代碼形式。
私用:您可以在內(nèi)部項目中使用它,無需對外公開。
必須遵守的唯一條件
雖然 MIT 許可證非常自由,但它附帶一個簡單但必須嚴格遵守的條件:您必須在軟件的所有副本或重要組成部分中包含原作者的版權(quán)聲明和許可證聲明 。

?? 主要用途

pugixml 是一個輕量級、簡單且快速的C++ XML處理庫。它的核心優(yōu)勢在于:

  • DOM-like 接口:提供直觀的方式來遍歷、修改XML文檔樹。
  • 極高的解析速度:其非驗證性XML解析器性能非常出色,適合性能敏感的場景。
  • XPath 支持:內(nèi)置了XPath 1.0的實現(xiàn),可以執(zhí)行復(fù)雜的數(shù)據(jù)查詢。
  • Unicode 支持:完美支持多種Unicode編碼,并能自動處理編碼轉(zhuǎn)換。

由于其高效和易用的特性,它被廣泛應(yīng)用于各種項目中,例如3D模型導(dǎo)入庫 Assimp、部分 Qt 模塊,以及許多自定義的游戲引擎和工具鏈中。

?? 基于源碼數(shù)據(jù)的核心示例解讀

當您獲取到本地文檔和示例后,可以結(jié)合源碼中的 resources/ 目錄下的XML數(shù)據(jù)文件(如 tree.xml),重點學習以下幾個經(jīng)典案例。這些案例涵蓋了90%的日常使用場景。

示例1:遍歷解析樹 (samples/traverse.cpp)

  • 目的:學習如何從文件加載XML,并遍歷所有節(jié)點。
  • 關(guān)鍵步驟與核心代碼
    • 加載XMLpugi::xml_document doc; doc.load_file("tree.xml");
    • 獲取根節(jié)點pugi::xml_node root = doc.child("root");
    • 遍歷子節(jié)點:使用 for (pugi::xml_node node = root.first_child(); node; node = node.next_sibling()) 循環(huán)處理每個節(jié)點。
    • 訪問屬性:通過 node.attribute("name").value() 獲取屬性值。
  • 應(yīng)用場景:配置文件讀取、數(shù)據(jù)結(jié)構(gòu)反序列化。

示例2:使用XPath查詢 (samples/xpath.cpp)

  • 的:學習用XPath語言快速定位復(fù)雜XML中的元素。
  • 關(guān)鍵步驟與核心代碼:
    • 選擇單個節(jié)點:pugi::xpath_node tool = doc.select_node("//tool[@id='1']");
    • 選擇節(jié)點集:pugi::xpath_node_set tools = doc.select_nodes("//tool[@timeout>0]");
    • 遍歷結(jié)果:for (auto& node : tools) 處理每一個匹配的節(jié)點。
    • 應(yīng)用場景:數(shù)據(jù)抽取、自動化測試驗證、復(fù)雜查詢。

示例3:修改XML文檔 (samples/modify.cpp)

  • 目的:學習如何添加/刪除節(jié)點和屬性,并保存文檔。
  • 關(guān)鍵步驟與核心代碼:
    • 添加節(jié)點:pugi::xml_node node = doc.append_child("new-node");
    • 添加屬性:node.append_attribute("name") = "value";
    • 刪除元素:doc.remove_child(node_to_delete);
    • 保存文檔:doc.save_file("output.xml");
  • 應(yīng)用場景:動態(tài)生成配置文件、XML編輯器、數(shù)據(jù)持久化。

?? windows x64依賴庫及頭文件

include:pugiconfig.hpp   pugixml.hpp

lib:pugixml.lib

?? 使用案例源碼

#include <iostream>
#include "pugixml/pugixml.hpp"
int traverse_base()
{
    pugi::xml_document doc;
    if (!doc.load_file("D:/Code/TestBin/appdata/testdata/xgconsole.xml")) return -1;
    pugi::xml_node tools = doc.child("Profile").child("Tools");
    // tag::basic[]
    for (pugi::xml_node tool = tools.first_child(); tool; tool = tool.next_sibling())
    {
        std::cout << "Tool:";
        for (pugi::xml_attribute attr = tool.first_attribute(); attr; attr = attr.next_attribute())
        {
            std::cout << " " << attr.name() << "=" << attr.value();
        }
        std::cout << std::endl;
    }
    // end::basic[]
    std::cout << std::endl;
    // tag::data[]
    for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool"))
    {
        std::cout << "Tool " << tool.attribute("Filename").value();
        std::cout << ": AllowRemote " << tool.attribute("AllowRemote").as_bool();
        std::cout << ", Timeout " << tool.attribute("Timeout").as_int();
        std::cout << ", Description '" << tool.child_value("Description") << "'\n";
    }
    // end::data[]
    std::cout << std::endl;
    // tag::contents[]
    std::cout << "Tool for *.dae generation: " << tools.find_child_by_attribute("Tool", "OutputFileMasks", "*.dae").attribute("Filename").value() << "\n";
    for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool"))
    {
        std::cout << "Tool " << tool.attribute("Filename").value() << "\n";
    }
    // end::contents[]
}
int traverse_iter()
{
    pugi::xml_document doc;
    if (!doc.load_file("D:/Code/TestBin/appdata/testdata/xgconsole.xml")) return -1;
    pugi::xml_node tools = doc.child("Profile").child("Tools");
    // tag::code[]
    for (pugi::xml_node_iterator it = tools.begin(); it != tools.end(); ++it)
    {
        std::cout << "Tool:";
        for (pugi::xml_attribute_iterator ait = it->attributes_begin(); ait != it->attributes_end(); ++ait)
        {
            std::cout << " " << ait->name() << "=" << ait->value();
        }
        std::cout << std::endl;
    }
    // end::code[]
}
int xpath_query()
{
    pugi::xml_document doc;
    if (!doc.load_file("D:/Code/TestBin/appdata/testdata/xgconsole.xml")) return -1;
    // tag::code[]
        // Select nodes via compiled query
    pugi::xpath_query query_remote_tools("/Profile/Tools/Tool[@AllowRemote='true']");
    pugi::xpath_node_set tools = query_remote_tools.evaluate_node_set(doc);
    std::cout << "Remote tool: ";
    tools[2].node().print(std::cout);
    // Evaluate numbers via compiled query
    pugi::xpath_query query_timeouts("sum(//Tool/@Timeout)");
    std::cout << query_timeouts.evaluate_number(doc) << std::endl;
    // Evaluate strings via compiled query for different context nodes
    pugi::xpath_query query_name_valid("string-length(substring-before(@Filename, '_')) > 0 and @OutputFileMasks");
    pugi::xpath_query query_name("concat(substring-before(@Filename, '_'), ' produces ', @OutputFileMasks)");
    for (pugi::xml_node tool = doc.first_element_by_path("Profile/Tools/Tool"); tool; tool = tool.next_sibling())
    {
        std::string s = query_name.evaluate_string(tool);
        if (query_name_valid.evaluate_boolean(tool)) std::cout << s << std::endl;
    }
    // end::code[]
}
int xpath_error()
{
    pugi::xml_document doc;
    if (!doc.load_file("D:/Code/TestBin/appdata/testdata/xgconsole.xml")) return -1;
    // tag::code[]
        // Exception is thrown for incorrect query syntax
    try
    {
        doc.select_nodes("http://nodes[#true()]");
    }
    catch (const pugi::xpath_exception& e)
    {
        std::cout << "Select failed: " << e.what() << std::endl;
    }
    // Exception is thrown for incorrect query semantics
    try
    {
        doc.select_nodes("(123)/next");
    }
    catch (const pugi::xpath_exception& e)
    {
        std::cout << "Select failed: " << e.what() << std::endl;
    }
    // Exception is thrown for query with incorrect return type
    try
    {
        doc.select_nodes("123");
    }
    catch (const pugi::xpath_exception& e)
    {
        std::cout << "Select failed: " << e.what() << std::endl;
    }
    // end::code[]
}
int xpath_variables()
{
    pugi::xml_document doc;
    if (!doc.load_file("D:/Code/TestBin/appdata/testdata/xgconsole.xml")) return -1;
    // tag::code[]
        // Select nodes via compiled query
    pugi::xpath_variable_set vars;
    vars.add("remote", pugi::xpath_type_boolean);
    pugi::xpath_query query_remote_tools("/Profile/Tools/Tool[@AllowRemote = string($remote)]", &vars);
    vars.set("remote", true);
    pugi::xpath_node_set tools_remote = query_remote_tools.evaluate_node_set(doc);
    vars.set("remote", false);
    pugi::xpath_node_set tools_local = query_remote_tools.evaluate_node_set(doc);
    std::cout << "Remote tool: ";
    tools_remote[2].node().print(std::cout);
    std::cout << "Local tool: ";
    tools_local[0].node().print(std::cout);
    // You can pass the context directly to select_nodes/select_node
    pugi::xpath_node_set tools_local_imm = doc.select_nodes("/Profile/Tools/Tool[@AllowRemote = string($remote)]", &vars);
    std::cout << "Local tool imm: ";
    tools_local_imm[0].node().print(std::cout);
    // end::code[]
}
int xpath_select()
{
    pugi::xml_document doc;
    if (!doc.load_file("D:/Code/TestBin/appdata/testdata/xgconsole.xml")) return -1;
    // tag::code[]
    pugi::xpath_node_set tools = doc.select_nodes("/Profile/Tools/Tool[@AllowRemote='true' and @DeriveCaptionFrom='lastparam']");
    std::cout << "Tools:\n";
    for (pugi::xpath_node_set::const_iterator it = tools.begin(); it != tools.end(); ++it)
    {
        pugi::xpath_node node = *it;
        std::cout << node.node().attribute("Filename").value() << "\n";
    }
    pugi::xpath_node build_tool = doc.select_node("http://Tool[contains(Description, 'build system')]");
    if (build_tool)
        std::cout << "Build tool: " << build_tool.node().attribute("Filename").value() << "\n";
    // end::code[]
}
int modify_add()
{
    pugi::xml_document doc;
    // tag::code[]
    // add node with some name
    pugi::xml_node node = doc.append_child("node");
    // add description node with text child
    pugi::xml_node descr = node.append_child("description");
    descr.append_child(pugi::node_pcdata).set_value("Simple node");
    // add param node before the description
    pugi::xml_node param = node.insert_child_before("param", descr);
    // add attributes to param node
    param.append_attribute("name") = "version";
    param.append_attribute("value") = 1.1;
    param.insert_attribute_after("type", param.attribute("name")) = "float";
    // end::code[]
    doc.print(std::cout);
    return 0;
}
int modify_base()
{
    pugi::xml_document doc;
    if (!doc.load_string("<node id='123'>text</node><!-- comment -->", pugi::parse_default | pugi::parse_comments)) return -1;
    // tag::node[]
    pugi::xml_node node = doc.child("node");
    // change node name
    std::cout << node.set_name("notnode");
    std::cout << ", new node name: " << node.name() << std::endl;
    // change comment text
    std::cout << doc.last_child().set_value("useless comment");
    std::cout << ", new comment text: " << doc.last_child().value() << std::endl;
    // we can't change value of the element or name of the comment
    std::cout << node.set_value("1") << ", " << doc.last_child().set_name("2") << std::endl;
    // end::node[]
    // tag::attr[]
    pugi::xml_attribute attr = node.attribute("id");
    // change attribute name/value
    std::cout << attr.set_name("key") << ", " << attr.set_value("345");
    std::cout << ", new attribute: " << attr.name() << "=" << attr.value() << std::endl;
    // we can use numbers or booleans
    attr.set_value(1.234);
    std::cout << "new attribute value: " << attr.value() << std::endl;
    // we can also use assignment operators for more concise code
    attr = true;
    std::cout << "final attribute value: " << attr.value() << std::endl;
    // end::attr[]
    return 0;
}
int main() 
{
    //pugi::xml_document doc;
    //// 加載XML文件 (這里假設(shè)同級目錄下有一個名為 "data.xml" 的文件)
    //pugi::xml_parse_result result = doc.load_file("D:/Code/TestBin/appdata/testdata/utftest_utf8.xml");
    //if (!result) {
    //    std::cerr << "文件加載失敗: " << result.description() << std::endl;
    //    return -1;
    //}
    //// 使用XPath查詢所有timeout大于0的tool節(jié)點
    //pugi::xpath_node_set tools = doc.select_nodes("http://tool[@timeout > 0]");
    //for (pugi::xpath_node node : tools) {
    //    pugi::xml_node tool = node.node();
    //    std::cout << "工具名稱: " << tool.attribute("name").value()
    //        << ", 超時時間: " << tool.attribute("timeout").as_int() << std::endl;
    //}
    modify_add();
    std::cin.get();
    return 0;
}

到此這篇關(guān)于C++工具庫之PugiXML使用實戰(zhàn)指南的文章就介紹到這了,更多相關(guān)C++ PugiXML使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

灌南县| 攀枝花市| 高淳县| 甘德县| 扶沟县| 禄劝| 吉安市| 贡觉县| 仁怀市| 白朗县| 新竹市| 科技| 阿拉善左旗| 肃南| 区。| 沙河市| 左权县| 大厂| 富蕴县| 阿瓦提县| 中卫市| 博罗县| 常德市| 赤峰市| 施秉县| 湘乡市| 辛集市| 南部县| 卓资县| 宜宾县| 车致| 巴东县| 清徐县| 高唐县| 崇文区| 万山特区| 广水市| 伊吾县| 准格尔旗| 民勤县| 大姚县|