#include "stdafx.h"
#include "pugixml1.9/pugixml.hpp"
#include <iostream>
/*
test.xml格式
<?xml version="1.0" encoding="utf-8"?>
<Root ErrCode="0" Status="OK">
<child01 yes="1">01</child01>
</Root>
*/
int _tmain(int argc, _TCHAR* argv[])
{
std::string xmlTmp = "<Root ErrCode='0' Status='OK'></Root>";
//1、内存中
pugi::xml_document doc;
pugi::xml_parse_result ret = doc.load_string(xmlTmp.c_str());
if (ret.status != pugi::status_ok)
{
return -1;
}
pugi::xml_node xdec = doc.prepend_child(pugi::node_declaration);
xdec.append_attribute("version").set_value("1.0");
xdec.append_attribute("encoding").set_value("utf-8");
//2、读取xmlTmp中节点
pugi::xpath_node xRoot = doc.select_node("//Root");//单root
pugi::xml_node hxRoot01= xRoot.node();
//3、在root 插入子节点 并设置属性
pugi::xml_object_range<pugi::xml_node_iterator> child01 = hxRoot01.children();
//插入节点child01
pugi::xml_node childNode = hxRoot01.append_child("child01");
childNode.text().set("01"); //设置值
//【childNode】设置属性
pugi::xml_attribute attributeTmp = childNode.append_attribute("yes");
attributeTmp.set_value("1");
//4、打印
doc.print(std::cout);
//5、保存
doc.save_file("test.xml");
return 0;
}