c++ - C++、RapidXML:解析大文件
问题描述
我想解析一个大型 XML 文件(33000 行)。按照我的xml文件的结构:
<?xml version="1.0" encoding="UTF-8"?><Root_2010 xmlns:noNamespaceSchemaLocation="textpool_1.2.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" lang="de-DE">
<Textpool Version="V20.12.08">
<TextpoolList FontFamily="Standard" FontSize="16" FontStyle="normal" FontWeight="bold" SID="S1" TextCharacterLength="0" TextLength="135">
<Text>GlobalCommonTextBook</Text>
</SID_Name>
<TextpoolBlock>
<TextpoolRecord CharacterLengthCheck="Ok" Status="Released" StdTextCharacterLength="4" StdTextLength="???" TID="Txt0_0" TermCheck="NotChecked" TermCheckDescription="NotChecked" TextLengthCheck="Ok" fixed="true">
<IEC translate="no">
<Text/>
</IEC>
<ExplanationText/>
<Text>nein</Text>
</ShortText>
</Description>
<Creator>z0046abb</Creator>
</TextpoolRecord>
</TextpoolBlock>
</TextpoolList>
</Textpool>
</Root_2010>
该元素TextpoolList
存储两个部分。它的名称存储在第一个Text
元素中。其中TextpoolBlock
存储了几个条目。感兴趣的元素再次出现Text
。
我需要解析这个文件并Text
从特定文件中提取所有元素TextpoolList
以将其导出到另一个文件中。未来的前景是利用属性TextpoolList
和扫描条目添加到ShortText
. 这就是为什么我想使用一些 XMLParser。
我决定给 XMLRapid 一个机会。由于这个文件很大,我需要将一些数据从堆栈切换到堆。由于我真的不知道该怎么做,所以我向您寻求帮助。我尝试了与https://linuxhint.com/parse_xml_in_c__/类似的方法。
rapidxml::xml_document<> doc;
rapidxml::xml_node<>* root_node = NULL;
rapidxml::xml_node<>* block_node = NULL;
rapidxml::xml_node<>* record_node = NULL;
rapidxml::xml_node<>* text_node = NULL;
std::ifstream infile(file);
std::string line;
std::string tp_data;
while (std::getline(infile, line))
tp_data += line;
std::vector<char> tp_data_copy(tp_data.begin(), tp_data.end());
tp_data_copy.push_back('\0');
doc.parse<0>(&tp_data_copy[0]);
root_node = doc.first_node("TextpoolList");
for (rapidxml::xml_node<>* textpool_node = root_node->first_node("Textpool"); textpool_node; textpool_node = textpool_node->next_sibling())
{
for (rapidxml::xml_node<>* list_node = textpool_node->first_node("TextpoolList"); list_node; list_node = list_node->next_sibling())
{
for (rapidxml::xml_node<>* block_node = list_node->first_node("TextpoolBlock"); block_node; block_node = block_node->next_sibling())
{
for (rapidxml::xml_node<>* record_node = block_node->first_node("TextpoolRecord"); record_node; record_node = record_node->next_sibling())
{
for (rapidxml::xml_node<>* text_node = record_node->first_node("Text"); text_node; text_node = text_node->next_sibling())
{
std::cout << "record = " << text_node->value();
std::cout << std::endl;
}
std::cout << std::endl;
}
}
}
}
}
编辑:我以一种我认为数据会落在堆上的方式更改了我的代码,但我仍然得到同样的错误,宁愿将数据存储在堆上而不是堆栈上。
感谢您的所有想法!
解决方案
好吧,事情终于奏效了。这是我的日常:
rapidxml::xml_document<> doc;
rapidxml::xml_node<>* root_node = NULL;
rapidxml::xml_node<>* block_node = NULL;
rapidxml::xml_node<>* record_node = NULL;
rapidxml::xml_node<>* text_node = NULL;
rapidxml::xml_node<>* list_node = NULL;
std::ifstream infile(file);
std::string line;
std::string tp_data;
while (std::getline(infile, line))
tp_data += line;
std::vector<char> tp_data_copy(tp_data.begin(), tp_data.end());
tp_data_copy.push_back('\0');
doc.parse<0>(&tp_data_copy[0]);
root_node = doc.first_node("Root_2010");
for (rapidxml::xml_node<>* textpool_node = root_node->first_node("Textpool"); textpool_node; textpool_node = textpool_node->next_sibling())
{
for (rapidxml::xml_node<>* list_node = textpool_node->first_node("TextpoolList"); list_node; list_node = list_node->next_sibling())
{
for (rapidxml::xml_node<>* block_node = list_node->first_node("TextpoolBlock"); block_node; block_node = block_node->next_sibling())
{
for (rapidxml::xml_node<>* record_node = block_node->first_node("TextpoolRecord"); record_node; record_node = record_node->next_sibling())
{
for (rapidxml::xml_node<>* text_node = record_node->first_node("Text"); text_node; text_node = text_node->next_sibling())
{
std::cout << "record = " << text_node->value();
std::cout << std::endl;
}
std::cout << std::endl;
}
}
}
}
如果还有一些时间,我会尝试为那个文件读取问题找到一些解决方法。
推荐阅读
- python-3.x - 谷歌数据流传递数据存储键作为输入参数
- javascript - 使用 ReactJS 和 python Flask。一般的做法
- python - 我是编码新手,正在通过 Tim 的聊天机器人教程学习技术。我不知道如何解决这个错误
- javascript - 如何阻止我的 discord.js 机器人向输出发送垃圾邮件
- javascript - 对具有多个对象且没有键的 JSON 数组进行排序
- uwp - 尝试使用 SocketActivityTrigger 注册后台任务时,UWP 冻结
- c# - 如何将 zip 文件转换为 base64Binary
- python - 如何生成总和为 1 的三个变量的所有可能组合
- arrays - json:无法将数组解组为结构类型的 Go 值
- flutter - 使用 Flutter BLOC 监听事件而不是状态