/* 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; printf("Here I am!"); 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; } void insertResultNode(){ struct DllNode * cur; struct resultNode * p; struct resultNode * c; int curDepth; int headFlag=0; int sqnFlag=0; int i; /* int len=3; char tmpString[]="Book/Title/"; char *tmp[]={"Book","Title"}; */ cur=head->next; for(i=0;ielement); while((cur!=head)&&(strcmp(cur->element,tmp[i])!=0)){ cur=cur->next; printf("while loop cur=%s!\n",cur->element); } if(cur==head) { headFlag=1; printf("Here I am!2\n"); break; } else { if(i==0){ curDepth=cur->SentNo; printf("Here I am!3\n"); } else if((cur->SentNo-curDepth)==1){ printf("Here I am!31\n"); curDepth=cur->SentNo; } else { sqnFlag=1; printf("Here I am!4\n"); break; } } } printf("Here I am!5\n"); if((headFlag==0)&&(sqnFlag==0)) { c=(struct resultNode *)malloc(sizeof(struct resultNode)); p=resultHead->prev; printf("Here I am!7\n"); c->prev=p; c->next=resultHead; c->tag=(char *)malloc(sizeof(tmpString)); strcpy(c->tag,tmpString); c->tag=(char *)malloc(sizeof(tmpString)); strcpy(c->tag,tmpString); 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; } } //struct DllNode * xmlContentExtract(char * xmlPtr, char **tag, int num) { struct DllNode * xmlContentExtract(char * xmlPtr) { char buf[BUFSIZ]; // char **tmpTag; XML_Parser parser = XML_ParserCreate(NULL); int done; int len; int depth = 0; int i; init(); XML_SetUserData(parser, &depth); XML_SetElementHandler(parser, startElement, endElement); XML_SetCharacterDataHandler(parser, char_hndl); do { size_t len = fread(buf, 1, sizeof(buf), stdin); done = len < sizeof(buf); /* xmlPtr is a pointer, which points to the starting byte of the xml document len=strlen(xmlPtr); if (!XML_Parse(parser,xmlPtr, len, done)) { */ if (!XML_Parse(parser, buf, len, done)) { fprintf(stderr, "%s at line %d\n", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser)); return NULL; } } while (!done); XML_ParserFree(parser); insertResultNode(); /* for(i=0;i