使用tinyxml创建xml
我有一个问题。我需要创建以下 xml:
<?xml version="1.0" encoding="utf-8"?>
<MyApp value="5" name="me">
</MyApp>
使用 c++ 代码我执行了以下操作:
#include <iostream>
#include <string>
#include "tinyxml.h"
void main(){
TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "utf-8", "");
doc.LinkEndChild( decl );
TiXmlElement * root;
root = new TiXmlElement( "MyApp" );
root->SetAttribute("value","5");
root->SetAttribute("name","me");
doc.LinkEndChild( root );
doc.SaveFile( "madeByHand.xml" );
return 0;
}
我使用 g++ tinyxml.cpp、tinyxmlerror.cpp 和tinyxmlparser.cpp 对其进行编译。 当我想查看我创建的 xml 时。我有一个错误:它说它没有正确编写。我哪里错了?:)
I have a problem. I need to create a following xml:
<?xml version="1.0" encoding="utf-8"?>
<MyApp value="5" name="me">
</MyApp>
Using c++ code I did the following:
#include <iostream>
#include <string>
#include "tinyxml.h"
void main(){
TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "utf-8", "");
doc.LinkEndChild( decl );
TiXmlElement * root;
root = new TiXmlElement( "MyApp" );
root->SetAttribute("value","5");
root->SetAttribute("name","me");
doc.LinkEndChild( root );
doc.SaveFile( "madeByHand.xml" );
return 0;
}
I compile it using g++ tinyxml.cpp tinyxmlerror.cpp and tinyxmlparser.cpp.
When i want to see the xml i've created. i have an error: it says it's not corectly written. where am i wrong?:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在尝试使用 C++ 作为某种类型的脚本。您必须创建一个
main()
函数,其中放置生产代码。当然,编译完成后,您必须从命令行启动应用程序。
You are trying to use C++ as some type of a script. You have to create a
main()
function In which the productive code is placed.After the compilation, you have to start the application from the command line, of course.
也许,您错过了“#include”tinystr.h“”?
Probably, you have missed " #include "tinystr.h" "?
您必须将 tinyxmlparser.cpptinyxmlerror.cpptinyxml.cpptinyxml.htinystr.cpptinystr.h 添加到您的项目中
you must add tinyxmlparser.cpp tinyxmlerror.cpp tinyxml.cpp tinyxml.h tinystr.cpp tinystr.h to your project
以防万一有人需要这样的 XML 文件结构:
_
所以这里是代码:
_
_
以及如何使用循环(for / while)添加多个元素的选项:
_
_
这里是代码:
_
Just in case anyone will need such a structure of an XML-file:
_
So here is the code for this:
_
_
And an option how to add more then one element using loops (for / while):
_
_
And here is the code for this:
_