首先到RapidXML官网下载源码,把rapidxml.hpp,rapidxml_iterators.hpp,rapidxml_print.hpp,rapidxml_utils.hpp文件复制到自己的项目中就可以了
1、读取xml文件
原文件的内容如下
<?xml version="1.0" encoding="UTF-8" ?>
<document>222222222
<English name="name1" value="value">The world has many languages</English>
<Heavy>changshuhang;</Heavy>
<csh>Umlaut Element</csh>
<English1 name="name11" value="value1">The world has many langquagesq</English>
<English2 name="name12" value="val5ue">The world has many languagesq7</English>
<English3 name="name13" value="valu23e">The world has many languages1</English>
<English4 name="name14" value="val4ue">The world has many languages2</English>
<English5 name="name15" value="val7ue">The world has many languages3</English>
<English6 name="name17" value="va6lue">The world has many languages4</English>
<English7 name="name18" value="val9ue">The world has many languages5</English>
<English8 name="name19" value="val8ue">The world has many languages6</English>
<phonebook>
<!--one item behalfs one contacted person.-->
<item>
<name>miaomaio</name>
<addr>Shaanxi Xi'an</addr>
<tel>13759911917</tel>
<email>[email protected]</email>
</item>
<item>
<name>gougou</name>
<addr>Liaoning Shenyang</addr>
<tel>15840330481</tel>
<email>[email protected]</email>
</item>
<!--more contacted persons.-->
</phonebook>
</document>
读取xml文件并显示出来
file<> fdoc("E://demo.xml");
//std::cout << fdoc.data() << std::endl;
xml_document<> doc; // character type defaults to char
doc.parse<0>(fdoc.data()); // 0 means default parse flags
//cout << doc << endl;
psln(doc.name());
try {
doc.parse<0>((char*)fdoc.data()); //会改变参数的内容,tmpbuf的生命周期必须到解析完
}
catch (rapidxml::parse_error &e) {
err = "parse xml error. ";
err += e.what();
psln(err);
while(1);
}
//获取document
xml_node<>* root = doc.first_node();
psln(root->name());
visitorAll(root);
方法,使用递归的方法全部显示出来
//遍历所有的node文件
void visitorAll(xml_node<>* root)
{
xml_node<>* English = root->first_node();
cout << "-----------------------" << endl;
psln(English->name());
//遍历node
for (; English; English = English->next_sibling())
{
if (English->value_size() == NULL)
{
visitorAll(English);
}
else
{
cout << "####################" << endl;
log(English->name(), English->value());
xml_attribute<>* attribute = English->first_attribute();
//遍历attr
for (; attribute; attribute = attribute->next_attribute())
{
log(attribute->name(), attribute->value());
}
}
}
}
2、生成并保存xml文件
//生成xml文件并保存
void save(const char * path)
{
xml_document<> document;
xml_node<>* rot = document.allocate_node(rapidxml::node_pi, document.allocate_string("xml version='1.0' encoding='utf-8'"));
document.append_node(rot);
//生成根节点
xml_node<>* root = document.allocate_node(node_element, "document", NULL);
document.append_node(root);
//生成节点
xml_node<>* English = document.allocate_node(node_element, "English", "The world has many languages");
xml_attribute<>* att = document.allocate_attribute("name", "name1");
English->append_attribute(att);
xml_attribute<>* att1 = document.allocate_attribute("value", "value123");
English->append_attribute(att1);
root->append_node(English);
//生成一个array节点
xml_node<>* phonebook = document.allocate_node(node_element, "phonebook", NULL);
for (int i = 0; i < 3; i++)
{
xml_node<>* item = document.allocate_node(node_element, "item", NULL);
//name
xml_node<>* name = document.allocate_node(node_element, "name", "miaomaio");
item->append_node(name);
//addr
xml_node<>* addr = document.allocate_node(node_element, "addr", "Shaanxi Xi'an");
item->append_node(addr);
//tel
xml_node<>* tel = document.allocate_node(node_element, "tel", "13759911917");
item->append_node(tel);
//email
xml_node<>* email = document.allocate_node(node_element, "name", "[email protected]");
item->append_node(email);
phonebook->append_node(item);
}
root->append_node(phonebook);
std::ofstream out(path);
out << document;
}
生成后的样式如下
<?xml version='1.0' encoding='utf-8' ?>
<document>
<English name="name1" value="value123">The world has many languages</English>
<phonebook>
<item>
<name>miaomaio</name>
<addr>Shaanxi Xi'an</addr>
<tel>13759911917</tel>
<name>[email protected]</name>
</item>
<item>
<name>miaomaio</name>
<addr>Shaanxi Xi'an</addr>
<tel>13759911917</tel>
<name>[email protected]</name>
</item>
<item>
<name>miaomaio</name>
<addr>Shaanxi Xi'an</addr>
<tel>13759911917</tel>
<name>[email protected]</name>
</item>
</phonebook>
</document>
3、修改xml文件
xml_node<>* delnode = root->first_node("English");
xml_node<>* mynode = doc.allocate_node(node_element, "address", "河北");
root->insert_node(delnode, mynode);
root->remove_node(delnode);//先删除address节点