使用 Tinyxml 在 xml 中添加一个子子项
Add a subchild in xml using Tinyxml
嗨,我试图将子项添加到我的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 );
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 从链接列表c++中删除一个项目
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '