嗨,我试图将子项添加到我的XML生成的文件中。我一直在通读手册,但我不太清楚如何在我的C++代码中添加它。有什么想法吗?
当前代码:
#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" );
电流输出:
<?xml version="1.0" ?>
<myroot>
<parent>
<child>Welcome to my child</child>
</parent>
</myroot>
所需的 XML 文件:
<?xml version="1.0" ?>
<myroot>
<parent>
<child>Welcome to child
<subchild>Welcome to my child2</subchild>
</child>
</parent>
</myroot>
您可以重复这些行:
msg = new TiXmlElement( "child" );
msg->LinkEndChild( new TiXmlText( "Welcome to my child" ));
msgs->LinkEndChild( msg );
因此,例如在循环中:
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);
}
完整演示
#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");
}
结果:
<?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>
更新
到评论:
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;
}
结果:
<?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>
我发现了如何将这些行添加到我的代码中来做到这一点
TiXmlElement* msg2;
msg2 = new TiXmlElement( "subchild" );
msg2->LinkEndChild( new TiXmlText( "Welcome to my child2" ));
msg->LinkEndChild( msg2 );