C++ XML文件读取

目录

一.RapidXml

二.环境配置

 三.使用方法及注意事项

1.主要函数

 2.XML文件读取代码

xml文件

 读取代码


一.RapidXml

         RapidXml是一个XML DOM解析工具包,适用于C++的xml解析器。整个解析器包含在rapidxml.hpp、rapidxml_utils.hpp、rapidxml_print.hpp、rapidxml_iterators.hpp这四个头文件中(前三个较常用),头文件获取方式及在线手册在以下链接。

快速XML (sourceforge.net)

二.环境配置

        新建工程->属性->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;
}

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,可以使用第三方库来读取和解析XML文件,比如 `pugixml`。以下是使用 `pugixml` 库读取XML文件的示例代码: 首先,需要下载并安装 `pugixml` 库。可以从 https://pugixml.org/ 下载源代码,并将 `pugixml.hpp` 和 `pugixml.cpp` 文件添加到你的C++项目中。 接下来,可以使用以下示例代码来读取XML文件: ```cpp #include <iostream> #include "pugixml.hpp" int main() { pugi::xml_document doc; if (!doc.load_file("data.xml")) { std::cout << "Failed to load 'data.xml'" << std::endl; return 1; } // 获取根节点 pugi::xml_node root = doc.child("root"); // 遍历子节点 for (pugi::xml_node node = root.first_child(); node; node = node.next_sibling()) { std::cout << "Node name: " << node.name() << std::endl; // 获取节点属性 std::cout << "Attribute value: " << node.attribute("attribute_name").value() << std::endl; // 获取节点文本内容 std::cout << "Node value: " << node.child_value() << std::endl; // 获取子节点 pugi::xml_node childNode = node.child("child_node"); if (childNode) { // 处理子节点 } } return 0; } ``` 确保将 `data.xml` 替换为你要读取的实际XML文件的路径。使用 `load_file` 函数可以加载XML文件,然后可以使用 `child`、`attribute`、`child_value` 等函数来获取节点的名称、属性和文本内容。 请注意,上述示例假设你已经将 `pugixml.hpp` 和 `pugixml.cpp` 文件添加到你的项目中,并且已经正确配置了编译器和链接器,以便能够正确编译和运行代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值