当前位置:首页 » 编程语言 » 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

热点内容
sm3杂凑算法 发布:2025-02-08 20:55:00 浏览:285
抽奖源码带后台 发布:2025-02-08 20:33:54 浏览:225
欧博中央空调原始密码是多少 发布:2025-02-08 20:33:47 浏览:335
运动使人快乐缓解压力 发布:2025-02-08 20:27:01 浏览:98
linux命令大文件 发布:2025-02-08 20:25:06 浏览:897
C蚁群算法 发布:2025-02-08 20:21:25 浏览:513
私人搭建服务器能干嘛 发布:2025-02-08 20:21:24 浏览:596
网吧怎么通过服务器玩网络游戏 发布:2025-02-08 19:59:52 浏览:914
文档编辑加密 发布:2025-02-08 19:56:31 浏览:393
phpmysql存储过程实例 发布:2025-02-08 19:54:40 浏览:162