pugixml操作

XML文件:

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

修改操作:
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 );
}

这里需要注意的是,ip节点的内容是一个pcdata类型的节点,这个节点的内容才是ip字符串,所以这里用text()来读写IP节点内容。    

如果要用value()方法得到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

取出属性值

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Profile FormatVersion="1">
    <Tools>
        <Tool Filename="jam" AllowIntercept="true">
            <Description>Jamplus build system</Description>
        </Tool>
        <Tool Filename="mayabatch.exe" AllowRemote="true" OutputFileMasks="*.dae" DeriveCaptionFrom="lastparam" Timeout="40" />
        <Tool Filename="meshbuilder_*.exe" AllowRemote="false" OutputFileMasks="*.mesh" DeriveCaptionFrom="lastparam" Timeout="10" />
        <Tool Filename="texbuilder_*.exe" AllowRemote="true" OutputFileMasks="*.tex" DeriveCaptionFrom="lastparam" />
        <Tool Filename="shaderbuilder_*.exe" AllowRemote="true" DeriveCaptionFrom="lastparam" />
    </Tools>
</Profile>

pugi::xml_document doc;
    if (!doc.load_file("xgconsole.xml")) return -1;

    pugi::xml_node tools = doc.child("Profile").child("Tools");

    //[code_traverse_base_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;
    }
//]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值