有道词典导出的xml中提取单词小工具

本文介绍了一个使用rapidxml库从XML文件中提取特定节点值并将其保存为文本文件的C++程序示例。通过实例展示了如何解析XML文档、获取根节点及子节点,并逐条读取所需数据。

先上界面:

核心代码,实际上是xml解析:

#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp"
#include "src\StdStrFile.h"

using namespace rapidxml;

int GetWordsFromXml(const _tstring& stXmlPath, _tstring& stTxtPath)
{
	if (!CStdFile::IfAccessFile(stXmlPath))
	{
		return -1;
	}

	std::string sXmlPath = CStdStr::ws2s(stXmlPath);

	file<> fdoc(sXmlPath.c_str());

	xml_document<> doc;
	doc.parse<0>(fdoc.data());

	//! 获取根节点
	rapidxml::xml_node<> *root = doc.first_node();

	//都是读取第一项,所以可以直接这样写
	//! 获取根节点第一个节点
	rapidxml::xml_node<>* node1 = root->first_node();
	char* first_name = node1->name();
	//std::cout<<node1->name()<<std::endl;
	rapidxml::xml_node<>* node11 = node1->first_node();
	char* second_name = node11->name();
	// 遍历students的子节点
	std::vector<_tstring> vWords;

	for (rapidxml::xml_node<> * node = root->first_node(first_name); node; node = node->next_sibling())
	{
		std::string sCurWord = node->first_node(second_name)->value();
		vWords.push_back(CStdStr::s2ws(sCurWord + '\n'));
	}

	return CStdFile::SaveTXTFile(stTxtPath, vWords);
}

更多的交流,欢迎留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值