我想解析一个很大的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。
我需要解析这个文件,并从特定的TextpoolList中提取所有Text元素,以便将其导出到另一个文件中。未来的前景是利用添加到ShortText中的TextpoolList和scan条目的属性。这就是为什么我想使用一些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;
}
}
}
}
}编辑:我修改了我的代码,我认为数据会放在堆上,但我仍然得到相同的错误,而不是将数据存储在堆上。
谢谢你所有的点子!
发布于 2021-05-05 02:30:39
好了,事情终于成功了。这是我的例程:
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;
}
}
}
}如果还剩一些时间,我会尝试找到一些解决文件读取问题的方法。
https://stackoverflow.com/questions/67373960
复制相似问题