tinyXml如何创建xml
您好,
我有以下内容:
class pers
{
public:
pers();
std::string name;
long favourite_number;
time_t curent_time;
}
我想使用tinyXml 创建一个xml。这是 xml:
<data>
<name> me </name>
<favourite_number> 1233336555 </favourite_number> //it's a long number
<curent_time> hour:day:month:year </curent_time>
这是代码:
main()
{
pers *p = new pers();
pers->name="me";
pers->favourite_number=12333336555;
/**/how too print the curent time ?**
TiXmlDocument doc;
TiXmlElement * root;
root = new TiXmlElement( "data" );
TiXmlElement * element1 = new TiXmlElement( "name" );
root->LinkEndChild( element1);
TiXmlText * text1 = new TiXmlText( pers->name );
element1->LinkEndChild( text1 );
TiXmlElement * element2 = new TiXmlElement( "favourite_number" );
root->LinkEndChild( element2);
long d=pers->favourite_number;
std::ostringstream os;
os << d;
std::string buf2=os.str();
TiXmlText * text2 = new TiXmlText( buf2 );
element2->LinkEndChild( text2 );
- 如果我想在 xml 中包含当前时间,该怎么做?
- 难道就没有更简单的方法吗?
HI,
I have the following:
class pers
{
public:
pers();
std::string name;
long favourite_number;
time_t curent_time;
}
I would like to create an xml using tinyXml. Here is the xml:
<data>
<name> me </name>
<favourite_number> 1233336555 </favourite_number> //it's a long number
<curent_time> hour:day:month:year </curent_time>
And here is the code:
main()
{
pers *p = new pers();
pers->name="me";
pers->favourite_number=12333336555;
/**/how too print the curent time ?**
TiXmlDocument doc;
TiXmlElement * root;
root = new TiXmlElement( "data" );
TiXmlElement * element1 = new TiXmlElement( "name" );
root->LinkEndChild( element1);
TiXmlText * text1 = new TiXmlText( pers->name );
element1->LinkEndChild( text1 );
TiXmlElement * element2 = new TiXmlElement( "favourite_number" );
root->LinkEndChild( element2);
long d=pers->favourite_number;
std::ostringstream os;
os << d;
std::string buf2=os.str();
TiXmlText * text2 = new TiXmlText( buf2 );
element2->LinkEndChild( text2 );
- How to do this if I want to have the current time in the xml?
- Isn't there an easier way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以你的问题基本上与 XML 无关,也与 TinyXml 无关?下次您可能想简化您的问题并为其指定合适的标题。
您可以通过 time.h 以及其中提供的功能或使用某些东西例如Boost.Date_Time。 此处也回答了一些问题。
So your question basically has nothing to do with XML, nor TinyXml? Next time you might want to simplify your question and give it an appropriate title.
You can either go via time.h and the functionality provided in there or use something like Boost.Date_Time. Something which was answered here as well.