我从xmlsoft.org下载了libxml2。我用它来解析xml文件。现在我想使用像这样的libxml创建我自己的xml文件......
<?xml version="1.0"?>
<Powersettings>
<PowerScheme>Testing</PowerScheme>
<CPUSpeed>Adaptive</CPUSpeed>
<Screensaver>No</Screensaver>
<LockScreentime>0</LockScreentime>
<Shutdownflag>Yes</Shutdownflag>
<DownloadsFlag>No</DownloadsFlag>
<PPasswordprotected>No</PPasswordprotected>
<Appsensorname>(None)</Appsensorname>
</Powersettings>
我使用gcc编译器,C语言编写程序,整个数据在一个Structure中。 我正在寻找一个好的教程或示例程序来使用libxml2创建上面的xml文档。
任何帮助?
答案 0 :(得分:3)
基于http://xmlsoft.org/examples/testWriter.c - 免责声明:我从未使用过libxml2,没有测试过,而且没有错误检测:
xmlTextWriterPtr writer;
writer = xmlNewTextWriterFilename("test.xml", 0);
xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
xmlTextWriterStartElement(writer, "Powersettings");
xmlTextWriterWriteElement(writer, "PowerScheme", "Testing"); xmlTextWriterEndElement(writer);
xmlTextWriterWriteElement(writer, "CPUSpeed", "Adaptive"); xmlTextWriterEndElement(writer);
/* ... etc ... */
xmlTextWriterEndElement(writer);
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);