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

如果还有一些时间,我会尝试为该文件读取操作找到一些解决方法。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...