/* This program reads an XML document from standard input. It parses the depth, element, attibute, value using the expat 1.0 written by Clark Cooper. And it also sets up a double linked list(DLL) and saves all parsed information in this DLL. */ #include #include #include #include #include "xmlparse.h" struct DllNode; //Declare the struct of Dll node struct DllNode { int SentNo; char * element; char * attribute; char * value; struct DllNode * prev; struct DllNode * next; }; struct resultNode { char * tag; char * attribute; char * value; struct resultNode * prev; struct resultNode * next; }; //typedef DllNode * DllNodePtr; //Dll node pointer struct DllNode * head; struct resultNode *resultHead; struct DllNode * temp; void init() { head=(struct DllNode*)malloc(sizeof(struct DllNode)); head->next=head; head->prev=head; head->SentNo=0; head->element=NULL; head->attribute=NULL; head->value=NULL; resultHead=(struct resultNode*)malloc(sizeof(struct resultNode)); resultHead->next=resultHead; resultHead->prev=resultHead; resultHead->tag=NULL; resultHead->attribute=NULL; resultHead->value=NULL; } struct DllNode * InsertElement(int SNum,const char * elmnt) { struct DllNode * p; struct DllNode * c; c=(struct DllNode *)malloc(sizeof(struct DllNode)); p=head->prev; c->prev=p; c->next=head; c->SentNo=SNum; c->element=(char *)malloc(sizeof(elmnt)); strcpy(c->element,elmnt); p->next=c; head->prev=c; return c; } void InsertAttribute(struct DllNode * d, const char * attr) { d->attribute=(char *)malloc( sizeof(attr)); strcpy(d->attribute,attr); } void InsertText(struct DllNode * d, char * val,int len) { int i; d->value=(char *)malloc(len); for(i=0; ivalue[i] =val[i]; } void PrintDll() { struct DllNode * curr; curr=head->next; while(curr!=head) { printf("%d: Element is %s, Attribute is %s\n",curr->SentNo,curr->element,curr->attribute); printf("\tValue is %s\n",curr->value); curr=curr->next; } } void PrintResultDll() { struct resultNode * curr; curr=resultHead->next; while(curr!=resultHead) { printf("tag is %s, Attribute is %s\n",curr->tag,curr->attribute); printf("\tValue is %s\n",curr->value); curr=curr->next; } } void startElement(void *userData, const char *name, const char **atts) { int i; int *depthPtr = userData; char *tmpAtts; temp=InsertElement(*depthPtr,name); for (i = 0; atts[i]; i += 2) InsertAttribute(temp,atts[i + 1]); *depthPtr += 1; } void char_hndl(void *data, const char *txt, int txtlen) { int *depthPtr = data; int i; char *txtPro; txtPro=(char *)malloc(txtlen); for(i=0; i2) InsertText(temp,txtPro,txtlen); } /* End char_hndl */ void endElement(void *userData, const char *name) { int *depthPtr = userData; *depthPtr -= 1; } int charcnt(char *string,int letter) { int count=0; while(*string){ if(*string==letter) count++; string++; } return (count); } char *getNext(char *wholeString) { char *ptr; char * remaindStr; if(strlen(wholeString)==0) return NULL; else { ptr=strchr(wholeString,'/'); remaindStr=ptr+1; return remaindStr; } } void getElements(char *string, int len, char **tmp ) { int i; int j; int parseLen; char *ptr; char *tmpString; ptr=strchr(string,'/'); parseLen=ptr-string; for(i=0;inext; for(i=0;ielement,tmp[i])!=0)){ cur=cur->next; } if(cur==head) { headFlag=1; break; } else { if(i==0){ curDepth=cur->SentNo; } else if((cur->SentNo-curDepth)==1){ curDepth=cur->SentNo; } else { sqnFlag=1; break; } } } if((headFlag==0)&&(sqnFlag==0)) { c=(struct resultNode *)malloc(sizeof(struct resultNode)); p=resultHead->prev; c->prev=p; c->next=resultHead; c->tag=(char *)malloc(sizeof(stringTag)); strcpy(c->tag,stringTag); if(cur->attribute!=NULL){ c->attribute=(char *)malloc(sizeof(cur->attribute)); strcpy(c->attribute,cur->attribute); } else c->attribute=NULL; c->value=(char *)malloc(sizeof(cur->value)); strcpy(c->value,cur->value); p->next=c; resultHead->prev=c; } // Free up memory for(i=0; i