int node_process(char cmd, mxml_node_t *tree, char *key, char *val, int num) { mxml_node_t *node = NULL; switch (cmd) { case 'a': node = xpath_create(tree, key, num); if(NULL != node) { // although node-text is empty, keep empty mxmlNewText(node, 0, val); } else { eprintf("%s exist\n", key); // return __LINE__; } break; case 'd': node = xpath_find(tree, key, num); if(NULL != node) { mxmlDelete(node); } break; case 'w': node = xpath_find(tree, key, num); if (node == NULL) { eprintf("not found: [%d]%s\n", num, key); return __LINE__; } if (NULL != node->child) { // printf("set\n"); mxmlSetText(node->child, 0, val); // mxmlSetTextf() doesn't work } else { mxmlNewText(node, 0, val); } break; case 'r': node = xpath_find(tree, key, num); if (NULL != node) { // mxml-2.8 vs mxml-2.7 -> (node->child) vs (node->child->value.text.string) // printf("%p\n", (node->child)); if (node->child == NULL) { eprintf("%s __got_empty_text__\n", key); return __LINE__; } else { printf("%d %s %s\n", num, key, trimsps(node->child->value.opaque)); } } else { eprintf("not found: [%d]%s\n", num, key); return __LINE__; } } return 0; }
const char* set_keyvalue(const char* xmlstr, const char* catelog_name, const char* key_name, const char* value) { if ((!catelog_name) || (!key_name) || (!value)) { DbgPrint("get_keyvalue input parameter error"); return (NULL); } mxml_node_t *xml; if (xmlstr == NULL) { xml = mxmlNewXML("1.0"); } else { xml = mxmlLoadString(NULL, xmlstr, MXML_TEXT_CALLBACK); } if (xml == NULL) { DbgPrint("xml load err: %s", xmlstr); return (NULL); } mxml_node_t *catelog = mxmlFindElement(xml, xml, catelog_name, NULL, NULL, MXML_DESCEND); if (catelog == NULL) { catelog = mxmlNewElement(xml, catelog_name); } mxml_node_t *node = mxmlFindElement(catelog,catelog,key_name,NULL,NULL,MXML_DESCEND); if (node) { mxmlSetText(node, 0, value); } else { node = mxmlNewElement(catelog, key_name); mxmlNewText(node, 0, value); } mxmlSetWrapMargin(2); char* system_xmlstr = strdup_tls(mxmlSaveAllocString(xml, MXML_TEXT_CALLBACK)); mxmlDelete(xml); return (system_xmlstr); }