void sax_cb(mxml_node_t *node, mxml_sax_event_t event, void *data) { const char *node_name = mxmlGetElement(node); if (event == MXML_SAX_DATA) { if (! strcmp(current_tag[tag_ptr], "DistanceMeters")) { if (! strcmp(current_tag[tag_ptr-1], "Lap")) { const char *x = mxmlGetText(node, NULL); lap_distances[lap_count] = atof(x); lap_count++; } } } if (event == MXML_SAX_ELEMENT_OPEN) { tag_ptr++; strcpy(current_tag[tag_ptr], node_name); if(! strcmp(node_name, "Activity")) { const char *t = mxmlElementGetAttr(node, "Sport"); strcpy(sport, t); } } if (event == MXML_SAX_ELEMENT_CLOSE) { tag_ptr--; } }
void parseCMD(int *clntSocket, char *buffer) { mxml_node_t *tree; printf("The xml was: %s\n", (char *) buffer); tree = mxmlLoadString(NULL, buffer, MXML_NO_CALLBACK); mxml_node_t *node = mxmlFindElement(tree, tree, "cmd", NULL, NULL, MXML_DESCEND); mxml_node_t *n_node = mxmlFindElement(node, tree, "name", NULL, NULL, MXML_DESCEND); mxml_node_t *arg1_node = mxmlFindElement(node, tree, "arg1", NULL, NULL, MXML_DESCEND); char * cmd = mxmlGetText(n_node, 0); char * arg = mxmlGetText(arg1_node, 0); printf("The cmd was: %s and has argument: %s\n", cmd, arg); int key = keyfromstring(cmd); switch (key) { case PULL: /* ... */ downloadFileCMD(); break; case PUSH: /* ... */ break; case MSG: /* ... */ break; case QUIT: /* ... */ break; default: printf("The cmd was %s", (char *)cmd); break; } //char *newMsg = ""; //send(*clntSocket, newMsg, strlen(newMsg), 0); }
int BCF_Read(bcf_t* bcf, const char* filename) { FILE *fp; mxml_node_t *node, *text; int i; if(bcf->filename != NULL) { free(bcf->filename); } bcf->filename = strdup(filename); fp = fopen(filename, "r"); if(fp == NULL) { fprintf(stderr, "Error opening config file %s for reading.\n", filename); return -1; } bcf->root = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK); fclose(fp); /* Now walk through the file and get all the children */ for(i = 0, node = mxmlFindElement(bcf->root, bcf->root, "child", NULL, NULL, MXML_DESCEND); node != NULL; node = mxmlFindElement(node, bcf->root, "child", NULL, NULL, MXML_DESCEND), i++ ) { text = mxmlGetFirstChild(node); bcf->entries[i] = strdup(mxmlGetText(text, NULL)); } bcf->num = i; /* Now read all the dongles */ for(i = 0, node = mxmlFindElement(bcf->root, bcf->root, "dongle", NULL, NULL, MXML_DESCEND); node != NULL; node = mxmlFindElement(node, bcf->root, "dongle", NULL, NULL, MXML_DESCEND), i++ ) { text = mxmlGetFirstChild(node); bcf->dongles[i] = strdup(mxmlGetText(text, NULL)); } bcf->numDongles = i; /* Clean everything up */ mxmlDelete(bcf->root); bcf->root = NULL; return 0; }
void main(void) { FILE *fp; char str[128]; const char *xml_path = "0:\\Zbin\\Utilities\\mxmltest\\test.xml"; fp = fopen(xml_path, "r"); if (fp == NULL) { sprintf(str, "Can't open file: %s", xml_path); ShowMSG(1, (int)str); return; } mxml_node_t *tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK); fclose(fp); if (tree == NULL) { ShowMSG(1, (int)"mxmlLoadFile = NULL"); return; } const char *log_path = "0:\\Zbin\\Log\\mxmltest.log"; fp = fopen(log_path, "a"); if (fp == NULL) { sprintf(str, "Can't open file: %s", log_path); ShowMSG(1, (int)str); mxmlDelete(tree); return; } mxml_node_t *node = tree; mxml_node_t *next = NULL; while (node != NULL) { switch (mxmlGetType(node)) { case MXML_ELEMENT: sprintf(str, "MXML_ELEMENT = %s\n", mxmlGetElement(node)); fwrite(str, sizeof(char), strlen(str), fp); break; case MXML_TEXT: sprintf(str, "MXML_TEXT = %s\n", mxmlGetText(node, 0)); fwrite(str, sizeof(char), strlen(str), fp); break; } next = mxmlGetFirstChild(node); if (next != NULL) { node = next; } else { next = mxmlGetNextSibling(node); if (next == NULL) { next = mxmlWalkNext(node, NULL, MXML_DESCEND); } node = next; } } fclose(fp); mxmlDelete(tree); }