c++tinyxml

Add a subchild in xml using Tinyxml


Hi im trying to add a subchild to my XML generated file. I've been reading through the manual and I can't quite figure out how to add it in my C++ code. Any ideas ?

Current code:

#include <Tinyxml.h>
TiXmlDocument doc;  
TiXmlElement* msg;
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );  
doc.LinkEndChild( decl );  

TiXmlElement * root = new TiXmlElement( "myroot" );  
doc.LinkEndChild( root );   

TiXmlElement * msgs = new TiXmlElement( "parent" );  
root->LinkEndChild( msgs );  

msg = new TiXmlElement( "child" );  
msg->LinkEndChild( new TiXmlText( "Welcome to my child" ));  
msgs->LinkEndChild( msg );  

doc.SaveFile( "tree.xml" );  

Current output:

<?xml version="1.0" ?>

<myroot>
    <parent>
        <child>Welcome to my child</child>
    </parent>
</myroot>

desired xml file:

<?xml version="1.0" ?>

<myroot>
    <parent>
        <child>Welcome to child
             <subchild>Welcome to my child2</subchild> 
        </child>
    </parent>
</myroot>

Solution

  • You can just repeat those lines:

    msg = new TiXmlElement( "child" );  
    msg->LinkEndChild( new TiXmlText( "Welcome to my child" ));  
    msgs->LinkEndChild( msg );  
    

    So, e.g. in a loop:

    for (int i = 0; i < 10; ++i) {
        std::string name = "child" + std::to_string(i);
        msg = new TiXmlElement(name);
        msg->LinkEndChild(new TiXmlText("Welcome to my " + name));
        msgs->LinkEndChild(msg);
    }
    

    Full Demo

    #include <tinyxml.h>
    
    int main() {
        TiXmlDocument doc;
        TiXmlElement* msg;
        TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", "");
        doc.LinkEndChild(decl);
    
        TiXmlElement* root = new TiXmlElement("myroot");
        doc.LinkEndChild(root);
    
        TiXmlElement* msgs = new TiXmlElement("parent");
        root->LinkEndChild(msgs);
    
        for (int i = 0; i < 10; ++i) {
            std::string name = "child" + std::to_string(i);
            msg = new TiXmlElement(name);
            msg->LinkEndChild(new TiXmlText("Welcome to my " + name));
            msgs->LinkEndChild(msg);
        }
    
        doc.SaveFile("tree.xml");
    }
    

    Result:

    <?xml version="1.0" ?>
    <myroot>
        <parent>
            <child0>Welcome to my child0</child0>
            <child1>Welcome to my child1</child1>
            <child2>Welcome to my child2</child2>
            <child3>Welcome to my child3</child3>
            <child4>Welcome to my child4</child4>
            <child5>Welcome to my child5</child5>
            <child6>Welcome to my child6</child6>
            <child7>Welcome to my child7</child7>
            <child8>Welcome to my child8</child8>
            <child9>Welcome to my child9</child9>
        </parent>
    </myroot>
    

    UPDATE

    To the comment:

    msg = new TiXmlElement("child");
    msg->LinkEndChild(new TiXmlText("Welcome to my child"));
    msgs->LinkEndChild(msg);
    
    for (int i = 1; i < 10; ++i) {
        std::string name = "child" + std::to_string(i);
        TiXmlElement* child = new TiXmlElement(name);
        child->LinkEndChild(new TiXmlText("Welcome to my " + name));
        msg->LinkEndChild(child);
    
        msg = child;
    }
    

    Result:

    <?xml version="1.0" ?>
    <myroot>
        <parent>
            <child>Welcome to my child
                <child1>Welcome to my child1
                    <child2>Welcome to my child2
                        <child3>Welcome to my child3
                            <child4>Welcome to my child4
                                <child5>Welcome to my child5
                                    <child6>Welcome to my child6
                                        <child7>Welcome to my child7
                                            <child8>Welcome to my child8
                                                <child9>Welcome to my child9</child9>
                                            </child8>
                                        </child7>
                                    </child6>
                                </child5>
                            </child4>
                        </child3>
                    </child2>
                </child1>
            </child>
        </parent>
    </myroot>