PUGIXML игнорирует элементы данных и узлы без атрибутов?

Я должен делать что-то в корне неправильно. У меня есть тестовая программа, настроенная для чтения и отображения содержимого файла XML, чтобы я мог изучать и изучать, как данные хранятся и представляются. У меня есть огромные XML-файлы, созданные в виде электронных таблиц, которые управляют нашим сборочным оборудованием. В конечном итоге мне нужно изменить порядок, редактировать и создавать данные в этих файлах.

Сейчас я просто пытаюсь выяснить, почему PUGIXML игнорирует данные узлов и узлы без атрибутов.

Это пример XML-файла, который я создал, когда стало ясно, что я не могу найти все в реальных файлах:

<Node1 attr11="attribute11"
    attr12="attribute12"
    attr13="attribute13"
    attr14="attribute14"
    attr15="attribute15"
    attr16="attribute16">
    <Node11 attr111="attribute111">
        <Value1>value1</Value1>
        <Value2>value2</Value2>
        <Value3>value3</Value3>
        <Value4>value4</Value4>
    </Node11>
    <Node12/>
    <Node13></Node13>
    <Node14 attr141="attribute141"
        attr142="attribute142"
        attr143="attribute143"
        attr144="attribute144">
        <Value1>value1</Value1>
        <Value2>value2</Value2>
        <Value3>value3</Value3>
        <Value4>value4</Value4>
    </Node14>
    <Node15>
        <Value1>value1</Value1>
        <Value2>value2</Value2>
        <Value3>value3</Value3>
        <Value4>value4</Value4>
    </Node15>
</Node1>

Вот частичный список оболочки C++, которую я создаю вокруг PUGIXML для совместимости с существующим кодом:

class XMLFileImporter2
{
public:

    // constructor
    XMLFileImporter2( string strIn )
    {
        // stash the file name
        m_strTheFileName = strIn;

        // make the call to the pugi system
        pugi::xml_parse_result result = m_xmlTheXML.load_file( m_strTheFileName.c_str() , pugi::parse_full );
        m_strLastResultMessage = string( result.description() );

        // did the xml read correctly?
        m_bDidFileRead = ( m_strLastResultMessage == "No error" );

        ....

    }

    void test1( vector<string> &lstText )
    {
        // clear the vector
        lstText.resize( 0 );

        // interate through the xml data
        for ( pugi::xml_node thisnode = m_xmlTheXML.first_child() ; thisnode ; thisnode = thisnode.next_sibling() )
        {
            testRecurse( thisnode , lstText , 0 );
        }
    }

    void testRecurse( pugi::xml_node nodeIn , vector<string> &lstText , int iLevelIn )
    {
        // first, increment a local copy of the level
        int iLocalLevel = iLevelIn + 1;

        // temp vars
        string strTemp , str1 , str2 , str3 , str4 , str5;
        int iAttr = 0;
        int iloop;

        // build the attribute list for THIS node
        for ( pugi::xml_attribute attr = nodeIn.first_attribute() ; attr ; attr = attr.next_attribute() )
        {
            // increment valid attribute count
            iAttr++;

            // init the string
            strTemp = CUtility::makeStringFromInt( iLocalLevel ) + " : " + CUtility::makeStringFromInt( iAttr ) + " : ";

            str1 = string( attr.name() );
            str2 = string( attr.value() );
            str3 = string( nodeIn.name() );

            pugi::xml_node_type tNodeType = nodeIn.type();
            str5 = getNodeType( tNodeType );

            str4 = string( nodeIn.value() );
            if ( str4 == "" ) str4 = "<blank>";


            strTemp = strTemp + "  Node Name = " + str3 + "  Node Type = " + str5 + "  Node Val = " + str4 + "  Attr Name = " + str1 + "  Attr Val = " + str2;

            for ( iloop = 0 ; iloop < iLevelIn ; iloop++ )
                strTemp = "    " + strTemp;

            // stash it
            lstText.push_back( strTemp );
        }

        // recurse to any child nodes
        for ( pugi::xml_node nextnode = nodeIn.first_child() ; nextnode ; nextnode = nextnode.next_sibling() )
        {
            testRecurse( nextnode , lstText , iLocalLevel );
        }
    }

    ...
};

Когда я создаю экземпляр моей обертки, он заставляет документ члена PUGIXML загрузить файл. Похоже, это происходит без каких-либо ошибок. После того, как я знаю, что это правда, я выполняю метод обхода теста члена обертки, чтобы выгрузить всю структуру дерева узла в вектор строки STL, и я дам это в несортированный список. Вот что показывает мой список:

1 : 1 :   Node Name = Node1  Node Type = node_element  Node Val = <blank>  Attr Name = attr11  Attr Val = attribute11
1 : 2 :   Node Name = Node1  Node Type = node_element  Node Val = <blank>  Attr Name = attr12  Attr Val = attribute12
1 : 3 :   Node Name = Node1  Node Type = node_element  Node Val = <blank>  Attr Name = attr13  Attr Val = attribute13
1 : 4 :   Node Name = Node1  Node Type = node_element  Node Val = <blank>  Attr Name = attr14  Attr Val = attribute14
1 : 5 :   Node Name = Node1  Node Type = node_element  Node Val = <blank>  Attr Name = attr15  Attr Val = attribute15
1 : 6 :   Node Name = Node1  Node Type = node_element  Node Val = <blank>  Attr Name = attr16  Attr Val = attribute16
    2 : 1 :   Node Name = Node11  Node Type = node_element  Node Val = <blank>  Attr Name = attr111  Attr Val = attribute111
    2 : 1 :   Node Name = Node14  Node Type = node_element  Node Val = <blank>  Attr Name = attr141  Attr Val = attribute141
    2 : 2 :   Node Name = Node14  Node Type = node_element  Node Val = <blank>  Attr Name = attr142  Attr Val = attribute142
    2 : 3 :   Node Name = Node14  Node Type = node_element  Node Val = <blank>  Attr Name = attr143  Attr Val = attribute143
    2 : 4 :   Node Name = Node14  Node Type = node_element  Node Val = <blank>  Attr Name = attr144  Attr Val = attribute144

Кажется, что любой пустой узел и любой узел без каких-либо явных атрибутов теряются. Я вызвал parse_full load_file. Я, должно быть, неправильно понимаю что-то очень простое здесь....

1 ответ

Хотя я не уверен, что понимаю, что вы пытаетесь сделать, в этом цикле вы печатаете только строки:

    for ( pugi::xml_attribute attr = nodeIn.first_attribute() ; attr ; attr = attr.next_attribute() )

Этот цикл выполняется только в том случае, если у узла есть один или несколько атрибутов, поэтому имеет смысл не видеть вывод для узлов без атрибутов.

Другие вопросы по тегам