先上界面:
核心代码,实际上是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);
}
更多的交流,欢迎留言。