0

I must be doing something fundamentally wrong. I have a test program set up to read and display an xml file's contents so that I can study and learn how the data is stored and represented. I have enormous spreadsheet-generated xml files that drive our assembly equipment. Ultimately I need to re-order, edit, and create data in those files.

For now I'm just trying to figure out why PUGIXML seems to be ignoring node data and nodes without attributes.

This is the sample xml file I created when it became clear I wasn't finding everything in the real files:

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

Here is a partial listing of the C++ wrapper I'm creating around PUGIXML for compability with existing code:

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

    ...
};

When I create an instance of my wrapper, it causes the member PUGIXML document to load the file. This appears to be happening without any errors. After I know this is true, I execute the wrapper's member test traverse method to dump the entire node tree structure to the STL string vector, and I dump that to an unsorted list box. This is what my list box shows:

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

Any empty node and any node without any explicit attributes seems to be getting lost. I've invoked the parse_full load_file option. I must be misunderstanding something very basic here....

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

While I'm not sure I understand what you're trying to do, you're only printing lines in this loop:

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

This loop only executes if a node has one or more attributes so it makes sense that you don't see any output for nodes without attributes.

zeuxcg
  • 9,216
  • 1
  • 26
  • 33
  • Yup, I see that. I came back to answer my own question. Spent a few days on something else, came back to this, and hit myself over the head a few times. Thanks. – user3307740 Feb 21 '14 at 22:36