C語言操作xml檔案的學習總結

2021-08-11 12:17:28 字數 3893 閱讀 6604

編譯命令非常簡單(注意:如果configure檔案沒有可執行許可權,增加可執行許可權):

./configure

make

make install

此時libxml2相關的標頭檔案應該在/usr/local/include/libxml2目錄下,libxml2相關的庫檔案應該在/usr/local/lib目錄下。

例子程式參考:

兩種方式讀寫xml:

操作xml彙總:

詳細例子:

例子程式如下:

實現獲取任意一條資訊和修改任意一條資訊。**有冗餘。

#include #include #include #include #include #define default_xml_file "registry.xml"

#define am (0)

#define fm (1)

/*typedef struct radioname;*/

typedef struct radiochannel;

//解析每乙個radio,提取出band、freq、redioname

static int parse_radio(xmldocptr doc, xmlnodeptr cur)

//獲取freq

if ((!xmlstrcmp(cur->name, (const xmlchar *)"freq")))

//獲取name

if ((!xmlstrcmp(cur->name, (const xmlchar *)"name")))

cur = cur->next;

}return 0;

}static int parse_radio_book(const char *file_name)

//獲取根節點

cur = xmldocgetrootelement(doc);

if (cur == null)

if ((xmlstrcmp(cur->name, (const xmlchar *)"radio_books")))

//遍歷處理根節點的每乙個子節點

cur = cur->xmlchildrennode;

while (cur != null)

else if(intcurband==1)

}/*getband getfreq */

if((!xmlstrcmp(cur->name, (const xmlchar *)(radio_last))))

/* if ((!xmlstrcmp(cur->name, (const xmlchar *)"radio"))) */

cur = cur->next;

}xmlfreedoc(doc);

return 0;

failed:

if (doc)

return -1;

}int getchannellist(int band)

//獲取根節點

cur = xmldocgetrootelement(doc);

if (cur == null)

if ((xmlstrcmp(cur->name, (const xmlchar *)"radio_books")))

//遍歷處理根節點的每乙個子節點

cur = cur->xmlchildrennode;

while (cur != null)

} cur = cur->next;

}xmlfreedoc(doc);

return 0;

failed:

if (doc)

return -1;

}radiochannel getlastchannel(int band,radiochannel radiochannel)

//獲取根節點

cur = xmldocgetrootelement(doc);

if (cur == null)

if ((xmlstrcmp(cur->name, (const xmlchar *)"radio_books")))

//遍歷處理根節點的每乙個子節點

cur = cur->xmlchildrennode;

while (cur != null)

} cur = cur->next;

}xmlfreedoc(doc);

return radiochannel;

failed:

if (doc)

return radiochannel;

}static int setinfo(const char *file_name,char* info,int band,int index,char* namechange)

//獲取根節點

cur = xmldocgetrootelement(doc);

if (cur == null)

if ((xmlstrcmp(cur->name, (const xmlchar *)"radio_books")))

//遍歷處理根節點的每乙個子節點

cur = cur->xmlchildrennode;

while (cur != null)

cur = cur->next;

}xmlfreedoc(doc);

return 0;

failed:

if (doc)

return -1;

}radiochannel getinfo(const char *file_name,char* info,int band,int index,radiochannel radiochannel)

//獲取根節點

cur = xmldocgetrootelement(doc);

if (cur == null)

if ((xmlstrcmp(cur->name, (const xmlchar *)"radio_books")))

//遍歷處理根節點的每乙個子節點

cur = cur->xmlchildrennode;

while (cur != null)

cur = cur->next;

}xmlfreedoc(doc);

return radiochannel;

failed:

if (doc)

return radiochannel;

}int main(int argc, char*argv)

; //radiochannel = getlastchannel(am,radiochannel);

//printf("band:%d\n",radiochannel.band);

//printf("band:%d\n",radiochannel.freq);

//printf("band:%s\n",radiochannel.name);

radiochannel radiochannel=;

radiochannel=getinfo(default_xml_file,"radio",0,3,radiochannel);

printf("band:%s\n",radiochannel.name);

setinfo(default_xml_file,"radio",am,3,"test");

/* char *xml_file = default_xml_file;

if (argc == 2)

if (parse_radio_book(xml_file) != 0)

*/ return 0;

}

<?xml version="1.0" encoding="utf-8"?>

C語言檔案操作總結

文字模式 二進位制模式 c可以認識ctrl z為檔案結尾 c只會把ctrl z當做檔案中的乙個字元,真正的檔案結尾在後面 ms dos用 r n組合表示換行符,c將 r n看作 n程式將看到這兩個字元 此外,檔案的第乙個字元可能不是檔案的第乙個字元,所以用文字檔案開啟,ftell 可以返回正確的檔案...

C語言檔案操作總結

幾乎每乙個專案都有乙個日誌記錄功能,用於記錄軟體執行的情況方便日後的審計與審查。而日誌記錄就涉及到了檔案的讀寫操作。因此,本文對c語言下的檔案操作進行了總結。fopen用來開啟檔案。file fopen const char name,const char mode name 是要開啟檔案的檔名指標...

c 操作xml檔案

dataset ds new dataset ds.readxml basepath config.xml 讀取xml檔案 label1.text ds.tables time rows 0 ta tostring 讀取xml檔案資料 ds.tables time rows 0 ta ta new ...