源码:
void WrittingXML(TiXmlString & xmlFile)
{
TiXmlDeclaration * xmlDec = new TiXmlDeclaration("1.0", "UTF-8", "yes");
TiXmlDocument * xmlDocs = new TiXmlDocument();
xmlDocs->LinkEndChild(xmlDec);
TiXmlElement * element = new TiXmlElement("Document");
xmlDocs->LinkEndChild(element);
TiXmlComment * comment = new TiXmlComment(" This is a list of new books ");
element->LinkEndChild(comment);
TiXmlElement * book = new TiXmlElement("Book");
book->SetAttribute("Name", "How to use TinyXML");
element->LinkEndChild(book);
TiXmlElement * author = new TiXmlElement("Author");
TiXmlText * Authortext = new TiXmlText("Leezhm");
author->LinkEndChild(Authortext);
book->LinkEndChild(author);
TiXmlElement * date = new TiXmlElement("Date");
TiXmlText * Datetext = new TiXmlText("2009-3-30");
date->LinkEndChild(Datetext);
book->LinkEndChild(date);
xmlDocs->SaveFile(xmlFile.c_str());
delete xmlDocs;
}
调用:
char szfile[] = "config.xml" ;
TiXmlString filename(szfile) ;
WrittingXML(filename) ;
生成的xml文件为:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Document>
<!-- This is a list of new books -->
<Book Name="How to use TinyXML">
<Author>Leezhm</Author>
<Date>2009-3-30</Date>
</Book>
</Document>