?? 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 + -