📄 parser.c
字号:
//====================================================================// 文件:parser.c// 描述:用于解释指定的专用xml文件,获取服务器信息以及文件信息// 作者:seeker// 日期:2007-06-19//====================================================================#include "nxml.h"#include <stdarg.h>// 下载地址结构typedef struct ServerUrl{ char *svr_url; // 服务器地址 char *svr_port; // 端口(保留) }ServerUrl,*PServerUrl;// xml包含的文件信息结构typedef struct FileInfo{ char *filehash; // 文件hash char *filename; // 文件名 char *playdatetime; // 节目时间 char *playlength; // 播放时长 char *coursename; // 课程名称 char *techer; // 主讲人、单位 char *tcobj; // 教学对象}FileInfo,*PFileInfo;//====================================================================/*void my_printf (char *str, ...){ va_list va; va_start (va, str); vfprintf (stderr, str, va); va_end (va);}*/int main (int argc, char **argv){ nxml_t *data; //xml文件对象 nxml_error_t ret; //函数执行结果 nxml_data_t *element, *tmp,*tmp2;//*tmp3; // 节点数据 // nxml_doctype_t *doctype;//编码类型 if (argc != 2) { fprintf (stderr, "Usage?\n\t%s url_xml\n\nExample:\n\t%s [file.xml|http://server/file.xml]\n\n", argv[0], argv[0]); return 1; } //初始化文件对象 ret = nxml_new (&data); if (ret != NXML_OK) { puts (nxml_strerror (ret)); return 1; } /*// 打印xml 文件信息: printf("FileName:%s\nSize:%d\nVersion:%d\nStandalone:%d\nEncoding:%s\n", data->file,data->size,data->version,data->standalone,data->encoding);*/ /* ret = nxml_set_func (data, my_printf);//??? if (ret != NXML_OK) { puts (nxml_strerror (ret)); return 1; }*/ /* if (!strncmp (argv[1], "http://", 7)) //判断是否http:// 开头 ret = nxml_parse_url (data, argv[1]); else*/ ret = nxml_parse_file (data, argv[1]);// 文件而已 if (ret != NXML_OK) { puts (nxml_strerror (ret)); return 1; } // 开始解释文件 // 根节点 nxml_root_element (data, &element);// 查找根节点 //printf ("根节点: %p: -%s-\n", element, element ? element->value : ""); printf ("根节点: -%s-\n", element ? element->value : ""); if (element)// 如果存在根节点 { tmp = element->children; while (tmp) { // char *hash,*completed,*leecher; printf ("\t子节点1: -%s-\n",tmp ? tmp->value : ""); // free(hash);free(completed);free(leecher); //----------------------------------- tmp2=tmp->children; // 打印2级节点和对应的参数值 PFileInfo pfi=(PFileInfo)malloc(sizeof(struct FileInfo)); memset(pfi,'\0',sizeof(struct FileInfo)); while(tmp2) { printf ("\t\t子节点2: -%s-\n",tmp2 ? tmp2->value : ""); if(strlen(tmp2->value)>0) //保证节点名不为空 { if( strcmp(tmp2->value,"filehash")==0) pfi->filehash=nxmle_find_attribute(tmp2,tmp2->value,&ret); if( strcmp(tmp2->value,"filename")==0) pfi->filename=nxmle_find_attribute(tmp2,tmp2->value,&ret); if( strcmp(tmp2->value,"playdatetime")==0) pfi->playdatetime=nxmle_find_attribute(tmp2,tmp2->value,&ret); if( strcmp(tmp2->value,"playlength")==0) pfi->playlength=nxmle_find_attribute(tmp2,tmp2->value,&ret); if( strcmp(tmp2->value,"coursename")==0) pfi->coursename=nxmle_find_attribute(tmp2,tmp2->value,&ret); if( strcmp(tmp2->value,"techer")==0) pfi->techer=nxmle_find_attribute(tmp2,tmp2->value,&ret); if( strcmp(tmp2->value,"tcobj")==0) pfi->tcobj=nxmle_find_attribute(tmp2,tmp2->value,&ret); } //================================ /* tmp3=tmp2->children; // 打印3级节点和对应的参数值 while(tmp3) { char *id,*ip,*uploaded,*download; id= nxmle_find_attribute(tmp3,"id",&ret); ip= nxmle_find_attribute(tmp3,"ip",&ret); uploaded= nxmle_find_attribute(tmp3,"uploaded",&ret); download= nxmle_find_attribute(tmp3,"downloaded",&ret); printf ("\t\t\t子节点3: -%s-\n",tmp3 ? tmp3->value : ""); printf (" \tid=%s\n\t\tip=%s\n\t\tup=%s\n\t\tdl=%s\n",id,ip,uploaded,download); free(id); free(ip); free(uploaded); free(download); tmp3 = tmp3->next; }*/ tmp2 = tmp2->next; }//while //================================ printf ("\n \tfilehash=%s\n\t\tfilename=%s\n\t\tplaydatetime=%s\n\t\tplaylength=%s\n\t\tcoursename=%s\n\t\ttecher=%s\n\t\ttcobj=%s\n\n", pfi->filehash,pfi->filename,pfi->playdatetime, pfi->playlength,pfi->coursename,pfi->techer,pfi->tcobj); free(pfi); //---------------------------------- tmp = tmp->next; } }/* // 查找指定节点! nxml_find_element (data, element, "welcome-file-list", &element); if (element && nxml_find_element (data, element, "welcome-file", &element) == NXML_OK && element) { printf ("\nwelcome-file: -%p- -%s-\n", element,element->value); } // 文件类型 nxml_doctype_element (data, &doctype); if (doctype) printf ("\nDoctype: -%s-%s-\n", doctype->name, doctype->value);*/ nxml_free (data); // 释放内存 return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -