使用tinyxml创建xml

发布于 2024-11-08 00:42:12 字数 759 浏览 0 评论 0原文

我有一个问题。我需要创建以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃兔兔 2024-11-15 00:42:13

您正在尝试使用 C++ 作为某种类型的脚本。您必须创建一个 main() 函数,其中放置生产代码。

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

当然,编译完成后,您必须从命令行启动应用程序。

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.

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

After the compilation, you have to start the application from the command line, of course.

把梦留给海 2024-11-15 00:42:13

也许,您错过了“#include”tinystr.h“”?

Probably, you have missed " #include "tinystr.h" "?

小糖芽 2024-11-15 00:42:13

您必须将 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

等你爱我 2024-11-15 00:42:12

以防万一有人需要这样的 XML 文件结构:

<?xml version="1.0" ?>
<Hello>Opening a new salutation
   <Greeting value="5" name="me"></Greeting>
</Hello>

_

所以这里是代码:

_

TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
doc.LinkEndChild( decl );

TiXmlElement* element = new TiXmlElement( "Hello" );
doc.LinkEndChild( element );

TiXmlText* text = new TiXmlText( "Opening a new salutation" );
element->LinkEndChild( text );

TiXmlElement* element2 = new TiXmlElement( "Greeting" );
element2->SetAttribute("value","5");
element2->SetAttribute("name","me");
element->LinkEndChild( element2 );

TiXmlText* text2 = new TiXmlText( "" );
element2->LinkEndChild( text2 );

doc.SaveFile( "madeByHand2.xml" );

_

以及如何使用循环(for / while)添加多个元素的选项:

_

<?xml version="1.0" ?>
<Hello>Opening a new salutation
    <Greeting value="5" name="me"></Greeting>
    <Greeting value="5" name="me"></Greeting>
    <Greeting value="5" name="me"></Greeting>
</Hello>

_

这里是代码:

_

TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
doc.LinkEndChild( decl );

TiXmlElement* element = new TiXmlElement( "Hello" );
doc.LinkEndChild( element );

TiXmlText* text = new TiXmlText( "Opening a new salutation" );
element->LinkEndChild( text );


for(long i=0; i<3; i++)
{
TiXmlElement* element2 = new TiXmlElement( "Greeting" );
TiXmlText* text2 = new TiXmlText( "" );

element2->SetAttribute("value","5");
element2->SetAttribute("name","me");

element->LinkEndChild( element2 );
element2->LinkEndChild( text2 );
}

doc.SaveFile( "madeByHand2.xml" );

Just in case anyone will need such a structure of an XML-file:

<?xml version="1.0" ?>
<Hello>Opening a new salutation
   <Greeting value="5" name="me"></Greeting>
</Hello>

_

So here is the code for this:

_

TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
doc.LinkEndChild( decl );

TiXmlElement* element = new TiXmlElement( "Hello" );
doc.LinkEndChild( element );

TiXmlText* text = new TiXmlText( "Opening a new salutation" );
element->LinkEndChild( text );

TiXmlElement* element2 = new TiXmlElement( "Greeting" );
element2->SetAttribute("value","5");
element2->SetAttribute("name","me");
element->LinkEndChild( element2 );

TiXmlText* text2 = new TiXmlText( "" );
element2->LinkEndChild( text2 );

doc.SaveFile( "madeByHand2.xml" );

_

And an option how to add more then one element using loops (for / while):

_

<?xml version="1.0" ?>
<Hello>Opening a new salutation
    <Greeting value="5" name="me"></Greeting>
    <Greeting value="5" name="me"></Greeting>
    <Greeting value="5" name="me"></Greeting>
</Hello>

_

And here is the code for this:

_

TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
doc.LinkEndChild( decl );

TiXmlElement* element = new TiXmlElement( "Hello" );
doc.LinkEndChild( element );

TiXmlText* text = new TiXmlText( "Opening a new salutation" );
element->LinkEndChild( text );


for(long i=0; i<3; i++)
{
TiXmlElement* element2 = new TiXmlElement( "Greeting" );
TiXmlText* text2 = new TiXmlText( "" );

element2->SetAttribute("value","5");
element2->SetAttribute("name","me");

element->LinkEndChild( element2 );
element2->LinkEndChild( text2 );
}

doc.SaveFile( "madeByHand2.xml" );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文