用c语言解析xml文件内容(C++读取xml文件)

本篇文章给大家谈谈用c语言解析xml文件内容,以及C++读取xml文件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

1、C语言xml解析2、c语言如何解析xml并将所有内容存入数组3、C# 解析 xml文件4、怎么用c语言解析xml文件

C语言xml解析

把所有的数据当做一个字符串

收到数据后先strstr(buffer,”?xml version=\”1.0\” encoding=\”UTF-8\”?”);

如果返回的是NULL则表示没有这段 退出

buffer是你收到的数据起始地址

c语言如何解析xml并将所有内容存入数组

/* 前段时间恰好做过类似的东西,代码可以给你参考下。

 *  Xml配置见最后

 */

typedef struct SrcFileFmt

{

    int   ColID;

    char  ColCode[64];      /* 字段英文名称 */

    char  ColName[128];     /* 字段中文名称*/

    char  ColType[20];      /* 字段类型(包含长度) */

    char  ColComment[128];  /* 字段描述 */

}SrcFileFmt;

int main(int argc, char **argv)

{

    SrcFileFmt SrcFileFmt[128];

    int iNum = -1;

    if ( 2  argc )

    {

        printf(“Usage: %s SrcXmlFile\n”, argv[0]);

        return -1;

    }

    iNum = parseSourceCfg(SrcCfgFile, SrcFileFmt);

    if (iNum == -1)

    {

        return -1;

    }

    return 0;

}

/* 调用此函数后,xml文件的内容会被存储到结构体数组SrcFileFmt srcfilefmt[]中

 * 此函数依赖于libxml2-2.9.2.tar.xz

 */

int parseSourceCfg(char *FileName, SrcFileFmt srcfilefmt[])

{ /* 解析源文件xml,FileName 为源xml文件名 */

    xmlDocPtr doc;

    xmlNodePtr cur, root;

    char sFileName[64] = {‘\0’};

    int cnt = 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, (const xmlChar *) “SrcRoot”))

    {

        xmlFreeDoc(doc);

        return -1;

    }

    

    cur = root-xmlChildrenNode;

    while (cur != NULL) 

    {

        if ((!xmlStrcmp(cur-name, (const xmlChar *)”Column”)))

        {

            xmlChar *key;

            xmlNodePtr cur_sub = cur;

            cur_sub = cur_sub-xmlChildrenNode;

            while (cur_sub != NULL) 

            {

                if ((!xmlStrcmp(cur_sub-name, (const xmlChar *)”ColID”))) {

                    key = xmlNodeListGetString(doc, cur_sub-xmlChildrenNode, 1);

                    killblank((char*)key);

                    srcfilefmt[cnt].ColID = atoi((char*)key);

                    xmlFree(key);

                }

                if ((!xmlStrcmp(cur_sub-name, (const xmlChar *)”ColCode”))) {

                    key = xmlNodeListGetString(doc, cur_sub-xmlChildrenNode, 1);

                    killblank((char*)key);

                    strcpy(srcfilefmt[cnt].ColCode, (char*)key);

                    xmlFree(key);

                }

                else if ((!xmlStrcmp(cur_sub-name, (const xmlChar *)”ColName”))) {

                    key = xmlNodeListGetString(doc, cur_sub-xmlChildrenNode, 1);

                    killblank((char*)key);

                    strcpy(srcfilefmt[cnt].ColName, (char*)key);

                    xmlFree(key);

                }

                else if ((!xmlStrcmp(cur_sub-name, (const xmlChar *)”ColType”))) {

                    key = xmlNodeListGetString(doc, cur_sub-xmlChildrenNode, 1);

                     killblank((char*)key);

                    strcpy(srcfilefmt[cnt].ColType, (char*)key);

                    xmlFree(key);

                }

                else if ((!xmlStrcmp(cur_sub-name, (const xmlChar *)”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); 

    return cnt;

}

SrcRoot

    Column

        ColID1/ColID

        ColCodekmh/ColCode

        ColName字段1/ColName

        ColTypeVARCHAR(11)/ColType

    /Column

    Column

        ColID2/ColID

        ColCodedfkmh/ColCode

        ColName字段2/ColName

        ColTypeVARCHAR(11)/ColType

    /Column

    Column

        ColID3/ColID

        ColCodehbh/ColCode

        ColName字段3/ColName

        ColTypeINTEGER(10)/ColType

    /Column

/SrcRoot

C# 解析 xml文件

private void btnShowXML_Click(object sender, EventArgs e)

{

string xmlNodeText = “测试结点”;

XmlDocument xmlDom = new XmlDocument();

xmlDom.Load(“.\\books.xml”);

XmlElement xmlRoot = xmlDom.DocumentElement;//根元素

TreeNode treeRoot = new TreeNode();

treeRoot.Text = xmlRoot.Name;

treeRoot.Tag = xmlRoot;

treeRoot.Nodes.Add(xmlNodeText);

tvShowXml.Nodes.Add(treeRoot);

}

需要创建一个xml文件,还需放置一个TreeView控件.(实现的效果是利用TreeView控件来读取xml文件中的内容,代码不全,只给思路)

怎么用c语言解析xml文件

我上次才给人写过

xml文件内容

?xml version=”1.0″ encoding=”UTF-8″ ?

– aicomoa_response

– country_list

– country

id7/id

pid0/pid

continent_id1/continent_id

guohao93/guohao

cntitle阿富汗/cntitle

entitleAfghanistan/entitle

hztitle阿富汗/hztitle

jptitleアフガニスタン/jptitle

kotitle??????/kotitle

jp_pinyinア/jp_pinyin

pinyinAFuHan/pinyin

sid0/sid

jibie1/jibie

/country

– country

id8/id

pid0/pid

continent_id2/continent_id

guohao355/guohao

cntitle阿尔巴尼亚/cntitle

entitleAlbania/entitle

hztitle阿尔巴尼亚/hztitle

jptitleアルバニア/jptitle

kotitle /

jp_pinyinア/jp_pinyin

pinyinAErBaNiYa/pinyin

sid0/sid

jibie1/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文件就行了

用c语言解析xml文件内容(C++读取xml文件)

用c语言解析xml文件内容的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于C++读取xml文件、用c语言解析xml文件内容的信息别忘了在本站进行查找喔。

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年4月2日 18:38:35
下一篇 2024年4月2日 18:45:43

相关推荐

  • c语言改写模式,c语言实现修改功能

    c语言程序修改? 1、这个程序有4个错误,我都加粗了,第一个是m没有赋初值,第二个是while表达式中的ch=getchar()需要括号括起来,第三个是m=m*10+ch-0中的0也需要用单引号括起来,第四个是第2个while中为m!=0。 2、define容易造成误会,因为不符合一般的编程习惯,false 0, true 1;scanf放在你的那个地方是达…

    2024年5月23日
    3900
  • c语言控制代码的换码序列,c语言交换代码

    求C语言编程大神解答一下下面这个编程代码? k==5,用5去除125余0,所以r=125%5中r为0。由于!0为1,所以执行while循环体:先打印出5(k的值),再n=n/k==125/5=25;由于251则再打印出*号。这一循环结果输出是5*。 下面是我的代码,三个函数分别对应三个问题。 在实现基本要求的前提下,拓展了可以从键盘输入的功能,以下为各题代码…

    2024年5月23日
    5600
  • linux给文件写权限,linux怎么给文件权限

    linux宝塔写入权限不足 1、可以使用chmod命令修改权限。语法:chmod [-cfvR] [–help] [–version] mode file…说明 : Linux/Unix 的档案调用权限分为三级 : 档案拥有者、群组、其他。利用 chmod 可以藉以控制档案如何被他人所调用。 2、通过更改文件权限可以使用c…

    2024年5月23日
    5100
  • c语言扫描io脚状态,c语言端口扫描

    求51单片机的上升沿和下降沿C语言检测程序列子,端口就是普通IO口。 上升沿触发是当信号有上升沿时的开关动作,当电位由低变高而触发输出变化的就叫上升沿触发。也就是当测到的信号电位是从低到高也就是上升时就触发,叫做上升沿触发。 单片机怎么计算1s内下降沿的个数的C语言程序或者计算两个下降沿的时间(检测脉冲频率)计算1s内下降沿的个数方法是,一个定时器设置定时1…

    2024年5月23日
    4400
  • c语言mallloc使用的简单介绍

    C语言中使用malloc必须加#includemallo.h? 1、在C语言中使用malloc函数进行动态内存分配。malloc的全称是memory allocation,中文叫动态内存分配。原型:extern void malloc(unsigned int num_bytes);功能:分配长度为num_bytes字节的内存块。 2、你可以看一下C语言那本…

    2024年5月23日
    4400
  • c语言三位小数,C语言三位小数

    怎样用C++语言输出精确到小数点后三位的数? 1、用C++语言输出精确到小数点后三位的数,可以参考下面给出的代码:coutsetiosflags(ios:fixed)setprecision(3)。其中 setiosflags中set是设置的意思。ios是iostream的缩写,即输入输出流。flags是标志的意思。 2、要精确到小数点后若干位,则数据类型为…

    2024年5月23日
    7300
  • c语言21点游戏,二十一点游戏代码c语言

    如何使用C语言编写简单小游戏? 1、数学知识:长方形的面积S=a*b 长方形周长L=2*(a+b)其中a b分别为长方形的宽和高。算法分析:长方形面积及周长均依赖于宽和高,所以先要输入宽高值,然后根据公式计算,输出结果即可。 2、/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有c语言的编译器(vc++0、turbo…

    2024年5月23日
    6400
  • c语言当中的null,C语言当中的符号

    C/C++中,NULL和null的区别是什么? nul 和 null要看编译器,不同的编译器有所区别。 所以C或者C++中都使用一个特殊定义NULL表示无效值,其本质就是未定义具体数据类型的0值。 null是是什么都没有的意思。在java中表示空对象。 本意是“空的;元素只有零的”意思。计算机中通常表示空值,无结果,或是空集合。\x0d\x0a在ASCII码…

    2024年5月23日
    4500
  • 包含c语言对txt文件命名的词条

    如何在C语言编程里面修改源文件名字 如果你是在WINDOWS的话,简单了,随便用个编辑器,比如记事本,然后写c源程序,保存到你想要保存的位置。如果你在DOS下,可以用edit,写好以后,按alt键,选择文件菜单,然后保存。 用open打开文件,注意操作模式使用“修改”或者“添加” 用write或者fprintf向文件中写入你的内容。 用close关闭文件。 …

    2024年5月23日
    4900
  • 学c语言编程,学c语言编程用什么软件

    编程开发必须要学C语言吗? 1、要学习。编程开发的学习内容主要包括c语言、python和c+语言。C语言作为一种简单灵活的高级编程语言,它是一个面向过程的语言,一般是作为计算机专业的基础入门语言课程。 2、C语言。对于刚接触编程的人来说,先学习C语言是非常重要的。C语言可以说是是计算机编程语言的鼻祖,其他的编程语言几乎全是由C语言变化衍生出来的。 3、不需要…

    2024年5月23日
    3500

发表回复

登录后才能评论



关注微信