2012-01-05 98 views
2

我從xmlsoft.org下載libxml2的。我用它來解析一個xml文件。現在我想用這樣相同的libxml創建自己的XML文件...如何使用libxml2生成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語言寫的程序和整個數據是一個結構。 我正在尋找一個好的教程或示例程序來創建XML文檔上面使用的libxml2。

任何幫助?

回答

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); 
+0

如何在xml庫[Libxml的testwriter類使用此代碼? – 2013-04-03 06:57:31