pugixml的簡單使用


一、簡介

pugixml的官方主頁為:http://pugixml.org/

pugixml是一個很棒的XML操作庫,

  • 它很輕量,只有三個文件(pugiconfig.hpp   pugixml.cpp  pugixml.hpp )
  • 支持Unicode
  • 支持XPATH解析
  • 速度快,僅比RapidXml慢一些
  • 跨平台(windows/linux)
  • 面向對象

 

       Xml庫解析性能比較表  

(表格來自:http://rapidxml.sourceforge.net/manual.html

2016.12.22更新:

pugixml官網上有了新的性能對比圖:

http://pugixml.org/benchmark.html

目前速度上已經比rapid_xml更快。(但我沒親自測試過)

 

二、配置

pugixml的三個文件,可以只include頭文件pugixml.hpp,CPP文件不用放到項目中,

方法是,在pugiconfig.hpp中:

// Uncomment this to switch to header-only version
 #define PUGIXML_HEADER_ONLY
 #include "pugixml.cpp"

  將這兩行的注釋去掉就可以了。

另外,如果項目使用的是Unicode設置,則可以在pugiconfig.hpp中:

// Uncomment this to enable wchar_t mode
 #define PUGIXML_WCHAR_MODE

  將wchar模式打開即可。

 

三、使用

XML文件:

<?xml version="1.0" encoding="GBK"?>
<root>
    <ip>192.168.1.1</ip>
<root>

C++:

void SaveToConfig( const wchar_t* xml_file, const wchar_t* ip )
{
	using namespace pugi;

	xml_document doc;
	xml_parse_result result = doc.load_file( xml_file );
	if (result.status != status_ok)
		return;

	if (!doc.child( L"root" ))
		doc.append_child( L"root" );

	xml_node node = doc.child( L"root" ).child( L"ip" );
	if (!node)
		doc.child( L"root" ).append_child( L"ip" )

	node.text().set( ip );

	doc.save_file( xml_file );
}

  這里需要注意的是,ip節點的內容是一個pcdata類型的節點,這個節點的內容才是ip字符串,所以這里用text()來讀寫IP節點內容。

如果要用.value()方法得到ip字符串的話,需要這樣用:

wstring ip = node.first_child().value();
node.first_child().set_value(L"10.10.10.10");

另外,node.text().set()方法也不錯,提供了常用的數據類型寫入XML的重載方法:

        // Set text (returns false if object is empty or there is not enough memory)
        bool set(const char_t* rhs);

        // Set text with type conversion (numbers are converted to strings, boolean is converted to "true"/"false")
        bool set(int rhs);
        bool set(unsigned int rhs);
        bool set(double rhs);
        bool set(bool rhs);

    #ifdef PUGIXML_HAS_LONG_LONG
        bool set(long long rhs);
        bool set(unsigned long long rhs);
    #endif

而node.text().as_xxx()方法可以按需要直接從XML文件中讀取出指定類型的數據:

        // Get text, or "" if object is empty
        const char_t* get() const;

        // Get text, or the default value if object is empty
        const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const;

        // Get text as a number, or the default value if conversion did not succeed or object is empty
        int as_int(int def = 0) const;
        unsigned int as_uint(unsigned int def = 0) const;
        double as_double(double def = 0) const;
        float as_float(float def = 0) const;

    #ifdef PUGIXML_HAS_LONG_LONG
        long long as_llong(long long def = 0) const;
        unsigned long long as_ullong(unsigned long long def = 0) const;
    #endif

實際上node.text()返回的是xml_text對象實例,上面的set()和as_xxx()是由xml_text實現的。

 

如果IP節點有屬性的話,可以遍歷屬性:

        for (pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute())  
        {  
            std::cout << " " << attr.name() << "=" << attr.value();  
        }  

 C++11:

	for (pugi::xml_attribute attr : node.attributes())
	{
		std::cout << " " << attr.name() << "=" << attr.value();
	}

作為讀取配置文件用,上面這些也差不多了,其它接口看看源碼就能明白怎樣用,pugixml提供了些高級用法,可以看他官網上提供的例子

 

四、注意事項

除了上面提到的<ip>節點內容為pcdata節點外,

關於中文的問題,clever101曾在pugixml庫的一個使用心得中提到,要用

std::locale::global(std::locale("chs"));  
const std::wstring strFilePath = _T(“c:\\ xgconsole.xml”);  
std::wifstream stream(strFilePath.c_str());  
pugi::xml_document doc;  
doc.load(stream);  

這種load stream的方式讀取,其實不必如此,只要保證文件保存時編碼為GB2312並且XML文件頭的聲明encoding="gb2312“就可以了。

<?xml version="1.0" encoding="gb2312"?>

  

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM