pugixml应用随笔

1.   修改元素值 second_node.set_value("miller");不对

  必须second_node.first_child().set_value("miller");

2. 小例子

#include <iostream>
#include "pugixml.hpp"
#include <stdio.h>

using namespace std;
using namespace pugi;


cout << "Hello world!" << endl; xml_document agentdoc; agentdoc.load_file("agent.xml"); xml_node root_node = agentdoc.child("root"); xml_node first_node = root_node.child("thrift"); xml_node second_node = first_node.child("switch"); cout<<"first name : "<<first_node.name()<<endl; cout<<"second name : "<<second_node.name()<<endl; cout<<"second value : "<<second_node.child_value()<<endl; 此处不能用value

// Note: For <node>text</node> node.value() does not return "text"! Use child_value() or text() methods to access text inside nodes.
// second_node.first_child().value(); second_node.first_child().set_value("qinbin"); cout<<"second value 执行完 : "<<second_node.child_value()<<endl; agentdoc.save_file("agent.xml");

3.

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模式打开即可。

4.一开始新生成xml时,second_node.text().set(var.data());

5.官网文档  http://pugixml.googlecode.com/svn/tags/latest/docs/quickstart.html#quickstart.main.modify 不翻墙不知道能不能打开

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;
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;
This is an example of adding new attributes/nodes to the document (samples/modify_add.cpp):

// 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";
This is an example of removing attributes/nodes from the document (samples/modify_remove.cpp):

// remove description node with the whole subtree
pugi::xml_node node = doc.child("node");
node.remove_child("description");

// remove id attribute
pugi::xml_node param = node.child("param");
param.remove_attribute("value");

// we can also remove nodes/attributes by handles
pugi::xml_attribute id = param.attribute("name");
param.remove_attribute(id);

 简单入门:http://blog.csdn.net/yukin_xue/article/details/7540011

6.头文件 添加encoding

 pugi::xml_document doc;
    pugi::xml_node decl = doc.prepend_child(pugi::node_declaration);
    decl.append_attribute("version") = "1.0";
    decl.append_attribute("encoding") = "UTF-8";
原文地址:https://www.cnblogs.com/qbmiller/p/3974455.html