目录
一.RapidXml
RapidXml是一个XML DOM解析工具包,适用于C++的xml解析器。整个解析器包含在rapidxml.hpp、rapidxml_utils.hpp、rapidxml_print.hpp、rapidxml_iterators.hpp这四个头文件中(前三个较常用),头文件获取方式及在线手册在以下链接。
二.环境配置
新建工程->属性->VC++目录->包含目录->添加包含上述头文件的文件夹。
三.使用方法及注意事项
1.主要函数
1.rapidxml::xml_document<> doc;//创建一个DOM tree对象
2.rapidxml::xml_node<> *node;//创建一个节点指针
3.rapidxml::xml_attribute<> *attribute;//创建一个属性
4.rapidxml::file<> fdoc("file name");//用file将XML文件读入缓存区,注意这里的file name要用文件的绝对路径
2.XML文件读取代码
xml文件
<ROOT>
<CONFIG>
<IP_NUMBER>192.168.249.123</IP_NUMBER> <!-- IP-number of the external socket -->
<PORT>49152</PORT> <!-- Port-number of the external socket -->
<SENTYPE>ImFree</SENTYPE> <!-- The name of your system send in <Sen Type="" > -->
<ONLYSEND>FALSE</ONLYSEND> <!-- TRUE means the client don't expect answers. Do not send anything to robot -->
</CONFIG>
<!-- RSI Data: TYPE= "BOOL", "STRING", "LONG", "DOUBLE" -->
<!-- INDX= "INTERNAL" switch on internal read values. Needed by DEF_... -->
<!-- INDX= "nmb" Input/Output index of RSI-Object / Maximum of RSI Channels: 64 -->
<!-- HOLDON="1", set this output index of RSI Object to the last value -->
<!-- DEF_Delay count the late packages and send it back to server -->
<!-- DEF_Tech: .C = advance .T = main run / .C1 advance set function generator 1 -->
<SEND>
<ELEMENTS>
<ELEMENT TAG="DEF_RIst" TYPE="DOUBLE" INDX="INTERNAL" />
<ELEMENT TAG="DEF_RSol" TYPE="DOUBLE" INDX="INTERNAL" />
<ELEMENT TAG="DEF_AIPos" TYPE="DOUBLE" INDX="INTERNAL" />
<ELEMENT TAG="DEF_ASPos" TYPE="DOUBLE" INDX="INTERNAL" />
<ELEMENT TAG="DEF_Delay" TYPE="LONG" INDX="INTERNAL" />
</ELEMENTS>
</SEND>
<RECEIVE>
<ELEMENTS>
<ELEMENT TAG="AK.A1" TYPE="DOUBLE" INDX="1" HOLDON="1" />
<ELEMENT TAG="AK.A2" TYPE="DOUBLE" INDX="2" HOLDON="1" />
<ELEMENT TAG="AK.A3" TYPE="DOUBLE" INDX="3" HOLDON="1" />
<ELEMENT TAG="AK.A4" TYPE="DOUBLE" INDX="4" HOLDON="1" />
<ELEMENT TAG="AK.A5" TYPE="DOUBLE" INDX="5" HOLDON="1" />
<ELEMENT TAG="AK.A6" TYPE="DOUBLE" INDX="6" HOLDON="1" />
</ELEMENTS>
</RECEIVE>
</ROOT>
读取代码
#include <iostream>
#include <io.h>
#include <string>
#include <fstream>
#include "rapidxml.hpp"
#include "rapidxml_utils.hpp"
#include "rapidxml_print.hpp"
using namespace std;
using namespace rapidxml;
int main()
{
//使用file读取xml文件
const char* file_name = "E:/xml/ros_rsi_ethernet.xml";
rapidxml::file<> fdoc(file_name);
cout << fdoc.data() << endl;
//创建DOM tree对象
rapidxml::xml_document<> doc;
//parse函数将文本解析为DOM tree;
doc.parse<0>(fdoc.data());
//获取根节点
rapidxml::xml_node<> *root = doc.first_node("ROOT");
//遍历CONFIG的子节点
for (rapidxml::xml_node<> *node = root->first_node("CONFIG")->first_node(); node; node = node->next_sibling())
{
if (node->first_node() != NULL)
{
cout << node->name() << " : " << node->value() << endl;
}
else
cout << "name: " << node->name() << "has no value" << endl;
for (rapidxml::xml_attribute<>* attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute())
cout << "attribute name = " << attribute->name() << "attribute value = " << attribute->value() << endl;
}
//遍历ELEMWNTS的子节点
rapidxml::xml_node<> *send = root->first_node("SEND");
for (rapidxml::xml_node<> *node = send->first_node("ELEMENTS")->first_node(); node; node = node->next_sibling())
{
if (node->first_node() != NULL)
{
cout << node->name() << " : " << node->value() << endl;
}
else
cout << "name: " << node->name() << "has no value" << endl;
for (rapidxml::xml_attribute<>* attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute())
cout << "attribute name = " << attribute->name() << "attribute value = " << attribute->value() << endl;
}
//遍历RECEIVE的子节点
rapidxml::xml_node<> *receive = root->first_node("RECEIVE");
for (rapidxml::xml_node<> *node = receive->first_node("ELEMENTS")->first_node(); node; node = node->next_sibling())
{
if (node->first_node() != NULL)
{
cout << node->name() << " : " << node->value() << endl;
}
else
cout << "name: " << node->name() << "has no value" << endl;
for (rapidxml::xml_attribute<>* attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute())
cout << "attribute name = " << attribute->name() << "attribute value = " << attribute->value() << endl;
}
return 0;
}