當前位置:首頁 » 編程語言 » c語言的xml解析

c語言的xml解析

發布時間: 2022-06-05 23:06:26

c語言解析XML文件

#include <string.h>
#include <stdio.h>

int main(int argc, char **argv)
{
if (argc != 2)
{
printf("Usage: <app> <filepath>\n");
return 1;
}
char szFileBuff[1024] = {0}, szBuff[1024];
FILE *fp;
char szName[64] = {0}, szId[64] = {0}, szSex[64] = {0}, szAge[64] = {0};
char *lFirst, *lEnd;

if ((fp = fopen(argv[1], "r")) == NULL)
{
printf("fopen %s file error!\n", argv[1]);
return 0;
}
while(fgets(szFileBuff, 1023, fp))
{
if ((lFirst = strstr(szFileBuff, "<name>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</name>");
memcpy(szName, lFirst + 6, lEnd - lFirst - 6);
}
if ((lFirst = strstr(szFileBuff, "<id>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</id>");
memcpy(szId, lFirst + 4, lEnd - lFirst - 4);
}
if ((lFirst = strstr(szFileBuff, "<sex>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</sex>");
memcpy(szSex, lFirst + 5, lEnd - lFirst - 5);
}
if ((lFirst = strstr(szFileBuff, "<age>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</age>");
memcpy(szAge, lFirst + 5, lEnd - lFirst - 5);
}
sprintf(szBuff, "name:%s;id:%s;sex:%s;age:%s", szName, szId, szSex, szAge);
printf("buff[%s]\n", szBuff);
}

fclose(fp);
return 0;
}

⑵ C語言xml解析

把所有的數據當做一個字元串
收到數據後先strstr(buffer,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
如果返回的是NULL則表示沒有這段 退出
buffer是你收到的數據起始地址

⑶ 使用c語言解析xml,有個結點描述一組信息,這個結點個數不確定,如何使用變數存下來,給另一個線程使用

我也遇到過一個類似的,不過當時沒搞出來。我當時是遍歷存儲的。
對了,你用的是哪個庫解析的?

⑷ 怎麼樣用c語言寫一個簡單的xml解析器

嗯,這個寫起來有點大,但思路簡單,因為xml的格式太固定啦,說白了,就是找到規律然後對整個文件逐行做字元串處理.............. 寫的時候,尤其是循環的時候,細心點,寫一點就查一下,注意索引啊......

⑸ 怎麼樣C語言解析一個XML文件中的信息,跪求高人指點。

你去網上下載一個開源庫tinyxml,順便看看教程,so easy

⑹ c語言如何解析xml並將所有內容存入數組

/*前段時間恰好做過類似的東西,代碼可以給你參考下。
*Xml配置見最後
*/

typedefstructSrcFileFmt
{
intColID;
charColCode[64];/*欄位英文名稱*/
charColName[128];/*欄位中文名稱*/
charColType[20];/*欄位類型(包含長度)*/
charColComment[128];/*欄位描述*/
}SrcFileFmt;

intmain(intargc,char**argv)
{
SrcFileFmtSrcFileFmt[128];
intiNum=-1;
if(2>argc)
{
printf("Usage:%sSrcXmlFile ",argv[0]);
return-1;
}
iNum=parseSourceCfg(SrcCfgFile,SrcFileFmt);
if(iNum==-1)
{
return-1;
}
return0;
}

/*調用此函數後,xml文件的內容會被存儲到結構體數組SrcFileFmtsrcfilefmt[]中
*此函數依賴於libxml2-2.9.2.tar.xz
*/
intparseSourceCfg(char*FileName,SrcFileFmtsrcfilefmt[])
{/*解析源文件xml,FileName為源xml文件名*/
xmlDocPtrdoc;
xmlNodePtrcur,root;
charsFileName[64]={''};
intcnt=0;
if(FileName==NULL)
{
return-1;
}
sprintf(sFileName,"%s.xml",FileName);
doc=xmlParseFile(sFileName);
if(doc==NULL)
{
return-1;
}
root=xmlDocGetRootElement(doc);
if(root==NULL){
xmlFreeDoc(doc);
return(-1);
}
if(xmlStrcmp(root->name,(constxmlChar*)"SrcRoot"))
{
xmlFreeDoc(doc);
return-1;
}

cur=root->xmlChildrenNode;
while(cur!=NULL)
{
if((!xmlStrcmp(cur->name,(constxmlChar*)"Column")))
{
xmlChar*key;
xmlNodePtrcur_sub=cur;
cur_sub=cur_sub->xmlChildrenNode;

while(cur_sub!=NULL)
{
if((!xmlStrcmp(cur_sub->name,(constxmlChar*)"ColID"))){
key=xmlNodeListGetString(doc,cur_sub->xmlChildrenNode,1);
killblank((char*)key);
srcfilefmt[cnt].ColID=atoi((char*)key);
xmlFree(key);
}
if((!xmlStrcmp(cur_sub->name,(constxmlChar*)"ColCode"))){
key=xmlNodeListGetString(doc,cur_sub->xmlChildrenNode,1);
killblank((char*)key);
strcpy(srcfilefmt[cnt].ColCode,(char*)key);
xmlFree(key);
}
elseif((!xmlStrcmp(cur_sub->name,(constxmlChar*)"ColName"))){
key=xmlNodeListGetString(doc,cur_sub->xmlChildrenNode,1);
killblank((char*)key);
strcpy(srcfilefmt[cnt].ColName,(char*)key);
xmlFree(key);
}
elseif((!xmlStrcmp(cur_sub->name,(constxmlChar*)"ColType"))){
key=xmlNodeListGetString(doc,cur_sub->xmlChildrenNode,1);
killblank((char*)key);
strcpy(srcfilefmt[cnt].ColType,(char*)key);
xmlFree(key);
}
elseif((!xmlStrcmp(cur_sub->name,(constxmlChar*)"ColComment"))){
key=xmlNodeListGetString(doc,cur_sub->xmlChildrenNode,1);
killblank((char*)key);
strcpy(srcfilefmt[cnt].ColComment,(char*)key);
xmlFree(key);
}
cur_sub=cur_sub->next;
}
cnt++;
}
cur=cur->next;
}
xmlFreeDoc(doc);
returncnt;
}

<SrcRoot>
<Column>
<ColID>1</ColID>
<ColCode>kmh</ColCode>
<ColName>欄位1</ColName>
<ColType>VARCHAR(11)</ColType>
</Column>
<Column>
<ColID>2</ColID>
<ColCode>dfkmh</ColCode>
<ColName>欄位2</ColName>
<ColType>VARCHAR(11)</ColType>
</Column>
<Column>
<ColID>3</ColID>
<ColCode>hbh</ColCode>
<ColName>欄位3</ColName>
<ColType>INTEGER(10)</ColType>
</Column>
</SrcRoot>

⑺ 怎麼樣C語言解析一個XML文件中的信息,題目很詳細,跪求高人指點。

這個要求不需要作XML的解析,用字元串搜索功能就足夠了,把網頁內容讀出之中按字元串搜索就可以找到<lat>和<lng>。
比如讓指針 char * page 指向讀取得到的網頁內容,就可以這樣得到經度lat和緯度lng:

#include <string.h>
#include <stdio.h>

double lat, lng;
char * str_lat, *str_lng;

str_lat = strstr(page, "<lat>"); /*搜索字元串<lat>的位置*/
sscanf(str_lat+5, "%lf", &lat); /*從搜索到的位置之後讀取一個浮點數作為緯度lat*/

str_lng = strstr(page, "<lng>");
sscanf(str_lng+5, "%lf", &lng); /*類似地,讀出經度lng*/

⑻ 怎麼用c語言解析xml文件

我上次才給人寫過
xml文件內容

<?xml version="1.0" encoding="UTF-8" ?>
- <aicomoa_response>
- <country_list>
- <country>
<id>7</id>
<pid>0</pid>
<continent_id>1</continent_id>
<guohao>93</guohao>
<cntitle>阿富汗</cntitle>
<entitle>Afghanistan</entitle>
<hztitle>阿富汗</hztitle>
<jptitle>アフガニスタン</jptitle>
<kotitle>??????</kotitle>
<jp_pinyin>ア</jp_pinyin>
<pinyin>AFuHan</pinyin>
<sid>0</sid>
<jibie>1</jibie>
</country>
- <country>
<id>8</id>
<pid>0</pid>
<continent_id>2</continent_id>
<guohao>355</guohao>
<cntitle>阿爾巴尼亞</cntitle>
<entitle>Albania</entitle>
<hztitle>阿爾巴尼亞</hztitle>
<jptitle>アルバニア</jptitle>
<kotitle />
<jp_pinyin>ア</jp_pinyin>
<pinyin>AErBaNiYa</pinyin>
<sid>0</sid>
<jibie>1</jibie>
</country>
</country_list>
</aicomoa_response>

運行結果

Info[0]=[id:7|pid:0|continent_id:1|guohao:93|cntitle:阿富汗|entitle:Afghanistan|
hztitle:阿富汗|jptitle:アフガニスタン|kotitle:??????|jp_pinyin:ア|pinyin:AFuHan|
sid:0|jibie:1|]
Info[1]=[id:7|pid:0|continent_id:1|guohao:93|cntitle:阿富汗|entitle:Afghanistan|
hztitle:阿富汗|jptitle:アフガニスタン|kotitle:??????|jp_pinyin:ア|pinyin:AFuHan|
sid:0|jibie:1|]
Press any key to continue

代碼

#include <stdio.h>
#include <string.h>
main()
{
int i=0;
FILE *fp;
char szFileBuff[1024] = {0}, szBuff[100][1024];
char id[10] = {0}, pid[10] = {0}, continent_id[10] = {0}, guohao[10] = {0},
cntitle[64]= {0},entitle[64]= {0},hztitle[64] = {0},jptitle[64] = {0},
kotitle[64] = {0},jp_pinyin[64] = {0}, pinyin[64] = {0},sid[10] = {0},jibie[10] = {0};
char *lFirst, *lEnd;

fp = fopen("country.txt","r");
if (fp==NULL)
{
printf("read XML file error!\n");
}
while(fgets(szFileBuff, 1023, fp))
{
if ((lFirst = strstr(szFileBuff, "<id>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</id>");
memcpy(id, lFirst + 4, lEnd - lFirst - 4);
}
if ((lFirst = strstr(szFileBuff, "<pid>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</pid>");
memcpy(pid, lFirst + 5, lEnd - lFirst - 5);
}
if ((lFirst = strstr(szFileBuff, "<continent_id>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</continent_id>");
memcpy(continent_id, lFirst + 14, lEnd - lFirst - 14);
}
if ((lFirst = strstr(szFileBuff, "<guohao>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</guohao>");
memcpy(guohao, lFirst + 8, lEnd - lFirst - 8);
}
if ((lFirst = strstr(szFileBuff, "<cntitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</cntitle>");
memcpy(cntitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<entitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</entitle>");
memcpy(entitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<hztitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</hztitle>");
memcpy(hztitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<jptitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</jptitle>");
memcpy(jptitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<kotitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</kotitle>");
memcpy(kotitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<jp_pinyin>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</jp_pinyin>");
memcpy(jp_pinyin, lFirst + 11, lEnd - lFirst - 11);
}
if ((lFirst = strstr(szFileBuff, "<pinyin>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</pinyin>");
memcpy(pinyin, lFirst + 8, lEnd - lFirst - 8);
}
if ((lFirst = strstr(szFileBuff, "<sid>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</sid>");
memcpy(sid, lFirst + 5, lEnd - lFirst - 5);
}
if ((lFirst = strstr(szFileBuff, "<jibie>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</jibie>");
memcpy(jibie, lFirst + 7, lEnd - lFirst - 7);
}
if ((lFirst = strstr(szFileBuff, "</country>")) != NULL)
{
sprintf(szBuff[i],"id:%s|pid:%s|continent_id:%s|guohao:%s|cntitle:%s|entitle:%s|hztitle:%s|jptitle:%s|kotitle:%s|jp_pinyin:%s|pinyin:%s|sid:%s|jibie:%s|",
id,pid,continent_id,guohao,cntitle,entitle,hztitle,jptitle,kotitle,jp_pinyin, pinyin,sid,jibie);
printf("Info[%d]=[%s]\n",i++, szBuff);
}
}
fclose(fp);
}

補充:你這個就說得太籠統了,
1 你上傳的xml文件具體格式是什麼?
2 要在網頁上顯示的具體格式是什麼
3 你根本不知道怎麼做 所以也不知道怎麼問
我不用關心你的c語言的cgi吧?我才不管是用什麼上傳的
只有你說的嵌入式三個字 給我一點有用信息 就是解析這個xml用插件恐怕是不行
只能C語言
4 我現在只要求你的xml文件格式和 網頁上要顯示哪些xml中解析出來的信息

只要知道這些 我只需要在我的程序上加上生成html文件就行了

linux下用C語言進行XML的組裝與解析

組裝就按照字元串組裝即可,解析使用動態鏈接庫解析xml消息。例如:xerces

⑽ 在linux上用c語言進行xml解析

它有個api叫xmlReadMemory可以從一塊內存緩沖區生成xmlDocPtr

熱點內容
行李箱的密碼鎖哪裡修 發布:2025-02-08 23:58:14 瀏覽:531
c語言字母ascii碼表 發布:2025-02-08 23:55:49 瀏覽:838
筆記本電腦一般存儲空間 發布:2025-02-08 23:51:15 瀏覽:835
php網站優化 發布:2025-02-08 23:49:41 瀏覽:455
php網頁列印 發布:2025-02-08 23:40:02 瀏覽:820
windowssmb無法訪問 發布:2025-02-08 23:33:28 瀏覽:467
python27編譯器 發布:2025-02-08 23:29:20 瀏覽:339
如何運行python代碼 發布:2025-02-08 23:28:15 瀏覽:692
新箱子密碼鎖怎麼設置 發布:2025-02-08 23:26:50 瀏覽:148
安卓如何可以看見被撤回的消息 發布:2025-02-08 23:19:17 瀏覽:798