Simple use of pugixml

Source: Internet
Author: User
Tags comparison table

I. Introduction

Pugixml official homepage is: http://pugixml.org/

Pugixml is a great XML operation library,

  • It is lightweight and has only three files (pugiconfig. hpp pugixml. cpp pugixml. hpp)
  • Unicode supported
  • Support for XPATH Parsing
  • Fast, only slower than RapidXml
  • Cross-platform (windows/linux)
  • Object-oriented

 

Xml database parsing performance comparison table

(Table from: http://rapidxml.sourceforge.net/manual.html)

 

Ii. Configuration

The three pugixml files can only include the header file pugixml. hpp, And the CPP file does not need to be placed in the project,

The method is in pugiconfig. hpp:

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

Remove the comments of the two rows.

In addition, if the project uses Unicode settings, you can go to pugiconfig. hpp:

// Uncomment this to enable wchar_t mode #define PUGIXML_WCHAR_MODE

Enable the wchar mode.

 

Iii. Use

XML file:

<?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 != xml_parse_status::status_ok )return;xml_node node = doc.child( L"root" ).child( L"ip" );node.text().set( ip );doc.save_file( xml_file );}

  Note that the content of an ip node is a pcdata node and the content of this node is an ip string. Therefore, text () is used to read and write the content of an IP node.

If you want to use the. value () method to obtain the ip string, use the following code:

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

In addition, the node. text (). set () method is also good, providing a common method for writing data types into 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

The node. text (). as_xxx () method can read the specified data type from the XML file as needed:

        // 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

In fact, node. text () returns an xml_text object instance. The above set () and as_xxx () are implemented by xml_text.

 

If an IP node has attributes, You can traverse the attributes:

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

 

For reading the configuration file, the above is almost the same. Other interfaces can see the source code to understand how to use it. pugixml provides some advanced usage. You can refer to the examples provided on his official website.

 

Iv. Notes

Except that the content of the <ip> node mentioned above is a pcdata node,

Regarding Chinese, clever101 once mentioned in a usage experience of the pugixml library that

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);  

This load stream method does not actually need to be read, as long as the file is savedEncoded as GB2312And the declaration of the XML File HeaderEncoding = "gb2312"You can.

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

  

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.