首页 > 解决方案 > 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;
                }
            }
        }
    }
    }

编辑:我以一种我认为数据会落在堆上的方式更改了我的代码,但我仍然得到同样的错误,宁愿将数据存储在堆上而不是堆栈上。

感谢您的所有想法!

标签: c++xmlrapidxml

解决方案


好吧,事情终于奏效了。这是我的日常:

    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;
                }
            }
        }
    }

如果还有一些时间,我会尝试为那个文件读取问题找到一些解决方法。


推荐阅读