int main(int argc, char**argv) { if (argv[1]) file = argv[1]; PValue v = xml_parse_file(file,true); if (value_is_error(v)) { fprintf(stderr,"error reading xml: %s\n",(char*)v); return 1; } //char *s = json_tostring(v); // try this for kicks... char *s = xml_tostring(v,0); printf("%s\n",s); obj_unref_v(v, s); // the same notation used for building up JSON data works fine for XML docs v = VAS("root", VAS("item",VMS("name","age","type","int"),"10"), VAS("item",VMS("name","name","type","string"),"Bonzo") ); s = xml_tostring(v,0); printf("%s\n",s); char *s2 = json_tostring(v); printf("json %s\n",s2); obj_unref_v(v, s, s2); printf("kount %d\n",obj_kount()); return 0; }
/*! Merge xml in filename into database * @retval -1 Error * @retval 0 Validation failed (with cbret set) * @retval 1 Validation OK */ static int load_extraxml(clicon_handle h, char *filename, const char *db, cbuf *cbret) { int retval = -1; cxobj *xt = NULL; int fd = -1; if (filename == NULL) return 1; if ((fd = open(filename, O_RDONLY)) < 0){ clicon_err(OE_UNIX, errno, "open(%s)", filename); goto done; } if (xml_parse_file(fd, "</config>", NULL, &xt) < 0) goto done; /* Replace parent w first child */ if (xml_rootchild(xt, 0, &xt) < 0) goto done; /* Merge user reset state */ retval = xmldb_put(h, (char*)db, OP_MERGE, xt, clicon_username_get(h), cbret); done: if (fd != -1) close(fd); if (xt) xml_free(xt); return retval; }
/* * load environment variable */ bool_t loadenv(char * file) { struct xml * root, * env; root = xml_parse_file(file); if(!root || !root->name) return FALSE; if(strcmp(root->name, "environment") != 0) { xml_free(root); return FALSE; } clearenv(); for(env = xml_child(root, "env"); env; env = env->next) { if(env->txt) putenv(env->txt); } xml_free(root); return TRUE; }
void *config_load(const char *fname) { xml_node_t *root; root = xml_parse_file(fname); if (!root) { /* XXX: do not use putlog here since it might not be initialized yet */ fprintf(stderr, "ERROR\n"); fprintf(stderr, "\tFailed to load config '%s': %s\n\n", fname, xml_last_error()); return NULL; } return(root); }
bool_t console_stdio_load(char * file) { struct xml * root; struct xml * in, * out, * err; root = xml_parse_file(file); if(!root || !root->name) return FALSE; if(strcmp(root->name, "console") != 0) { xml_free(root); return FALSE; } in = xml_get(root, "stdin", -1); if(! in) { xml_free(root); return FALSE; } out = xml_get(root, "stdout", -1); if(! out) { xml_free(root); return FALSE; } err = xml_get(root, "stderr", -1); if(! err) { xml_free(root); return FALSE; } if(! console_stdio_set(xml_attr(in, "name"), xml_attr(out, "name"), xml_attr(err, "name"))) { xml_free(root); return FALSE; } xml_free(root); return TRUE; }
/** int save_https_xml(int cofd, SSL* ssl, tList** pl, tXML** xml, char** recvfn, const char* wdir, int timeout, int* state) 受信した XML通信データをファイルに保存 @param cofd 接続相手へのソケット @param ssl 接続相手への SSLソケット.SSL通信でない場合は NULL を指定. @param[out] *pl 受信したヘッダが格納されるリストへのポインタ. @param[out] *xml パースされたデータが格納されるXML構造体へのポインタ @param[out] *recvfn 保存するファイル名 @param wdir 作業用ディレクトリを指定する.NULLの場合は /tmp になる. @param timeout 最初の受信までのタイムアウト(s) @param[out] state サーバとの接続状態.接続中なら TRUE.切断した場合は FALSE.NULLを指定しても良い. @retval 1以上 受信したファイルのサイズ(Byte)(ヘッダを含む) @retval 0 正常切断 @retval -1 受信エラー @retval RECV_TIMEOUTED タイムアウトした. */ int save_https_xml(int cofd, SSL* ssl, tList** pl, tXML** xml, char** recvfn, const char* wdir, int timeout, int* state) { int header; if (pl==NULL || recvfn==NULL) return 0; *pl = NULL; if (xml!=NULL) *xml = NULL; *recvfn = temp_filename(wdir, WORK_FILENAME_LEN); int cc = recv_https_file(cofd, ssl, pl, *recvfn, wdir, timeout, &header, state); if (cc<=0 || *pl==NULL) { free(*recvfn); *recvfn = NULL; if (cc==0) return 0; return -1; } if (header) { free(*recvfn); *recvfn = NULL; } else { if (xml!=NULL && *pl!=NULL && *recvfn!=NULL && cc>0) { Buffer buf = search_protocol_header(*pl, (char*)"Content-Type", 1); //application/llsd+xml; charset=UTF-8 //if (buf.buf!=NULL && strstrcase((const char*)buf.buf, "xml")!=NULL) { *xml = xml_parse_file(*recvfn); //if (*xml!=NULL && (*xml)->state!=XML_PARSED) del_xml(xml); if (*xml!=NULL && (*xml)->state<0) del_xml(xml); //} free_Buffer(&buf); } } return cc; }
/* Read selections from a common xml-file. Called when loading the plugin. * Returns TRUE if data has been read, FALSE if no data is available * or an error occurred. * This is analog to folder.h::folder_read_list. */ gboolean notification_foldercheck_read_array(void) { gchar *path; GNode *rootnode, *node, *branchnode; XMLNode *xmlnode; gboolean success = FALSE; path = foldercheck_get_array_path(); if(!is_file_exist(path)) { path = NULL; return FALSE; } /* We don't do merging, so if the file existed, clear what we have stored in memory right now.. */ notification_free_folder_specific_array(); /* .. and evaluate the file */ rootnode = xml_parse_file(path); path = NULL; if(!rootnode) return FALSE; xmlnode = rootnode->data; /* Check that root entry is "foldercheckarray" */ if(strcmp2(xmlnode->tag->tag, "foldercheckarray") != 0) { g_warning("wrong foldercheck array file"); xml_free_tree(rootnode); return FALSE; } /* Process branch entries */ for(branchnode = rootnode->children; branchnode != NULL; branchnode = branchnode->next) { GList *list; guint id; SpecificFolderArrayEntry *entry = NULL; xmlnode = branchnode->data; if(strcmp2(xmlnode->tag->tag, "branch") != 0) { g_warning("tag name != \"branch\""); return FALSE; } /* Attributes of the branch nodes */ list = xmlnode->tag->attr; for(; list != NULL; list = list->next) { XMLAttr *attr = list->data; if(attr && attr->name && attr->value && !strcmp2(attr->name, "name")) { id = notification_register_folder_specific_list(attr->value); entry = foldercheck_get_entry_from_id(id); /* We have found something */ success = TRUE; break; } } if((list == NULL) || (entry == NULL)) { g_warning("Did not find attribute \"name\" in tag \"branch\""); continue; /* with next branch */ } /* Now descent into the children of the brach, which are the folderitems */ for(node = branchnode->children; node != NULL; node = node->next) { FolderItem *item = NULL; /* These should all be leaves. */ if(!G_NODE_IS_LEAF(node)) g_warning("Subnodes in \"branch\" nodes should all be leaves. " "Ignoring deeper subnodes."); /* Check if tag is "folderitem" */ xmlnode = node->data; if(strcmp2(xmlnode->tag->tag, "folderitem") != 0) { g_warning("tag name != \"folderitem\""); continue; /* to next node in branch */ } /* Attributes of the leaf nodes */ list = xmlnode->tag->attr; for(; list != NULL; list = list->next) { XMLAttr *attr = list->data; if(attr && attr->name && attr->value && !strcmp2(attr->name, "identifier")) { item = folder_find_item_from_identifier(attr->value); break; } } if((list == NULL) || (item == NULL)) { g_warning("Did not find attribute \"identifier\" in tag " "\"folderitem\""); continue; /* with next leaf node */ } /* Store all FolderItems in the list */ /* We started with a cleared array, so we don't need to check if it's already in there. */ entry->list = g_slist_prepend(entry->list, item); } /* for all subnodes in branch */ } /* for all branches */ return success; }
/*! Load a configuration file to candidate database * Utility function used by cligen spec file * @param[in] h CLICON handle * @param[in] cvv Vector of variables (where <varname> is found) * @param[in] argv A string: "<varname> (merge|replace)" * <varname> is name of a variable occuring in "cvv" containing filename * @note that "filename" is local on client filesystem not backend. * @note file is assumed to have a dummy top-tag, eg <clicon></clicon> * @code * # cligen spec * load file <name2:string>, load_config_file("name2","merge"); * @endcode * @see save_config_file */ int load_config_file(clicon_handle h, cvec *cvv, cvec *argv) { int ret = -1; struct stat st; char *filename = NULL; int replace; cg_var *cv; char *opstr; char *varstr; int fd = -1; cxobj *xt = NULL; cxobj *x; cbuf *cbxml; if (cvec_len(argv) != 2){ if (cvec_len(argv)==1) clicon_err(OE_PLUGIN, 0, "Got single argument:\"%s\". Expected \"<varname>,<op>\"", cv_string_get(cvec_i(argv,0))); else clicon_err(OE_PLUGIN, 0, "Got %d arguments. Expected: <varname>,<op>", cvec_len(argv)); goto done; } varstr = cv_string_get(cvec_i(argv, 0)); opstr = cv_string_get(cvec_i(argv, 1)); if (strcmp(opstr, "merge") == 0) replace = 0; else if (strcmp(opstr, "replace") == 0) replace = 1; else{ clicon_err(OE_PLUGIN, 0, "No such op: %s, expected merge or replace", opstr); goto done; } if ((cv = cvec_find(cvv, varstr)) == NULL){ clicon_err(OE_PLUGIN, 0, "No such var name: %s", varstr); goto done; } filename = cv_string_get(cv); if (stat(filename, &st) < 0){ clicon_err(OE_UNIX, 0, "load_config: stat(%s): %s", filename, strerror(errno)); goto done; } /* Open and parse local file into xml */ if ((fd = open(filename, O_RDONLY)) < 0){ clicon_err(OE_UNIX, errno, "open(%s)", filename); goto done; } if (xml_parse_file(fd, "</clicon>", NULL, &xt) < 0) goto done; if (xt == NULL) goto done; if ((cbxml = cbuf_new()) == NULL) goto done; x = NULL; while ((x = xml_child_each(xt, x, -1)) != NULL) { /* Ensure top-level is "config", maybe this is too rough? */ xml_name_set(x, "config"); if (clicon_xml2cbuf(cbxml, x, 0, 0) < 0) goto done; } if (clicon_rpc_edit_config(h, "candidate", replace?OP_REPLACE:OP_MERGE, cbuf_get(cbxml)) < 0) goto done; cbuf_free(cbxml); // } ret = 0; done: if (xt) xml_free(xt); if (fd != -1) close(fd); return ret; }
XMLNODE *XMLParseFile(XSLTGLOBALDATA *gctx, char *file) { info("XMLParseFile:: file %s", file); return xml_parse_file(gctx, xml_strdup(file), 0); }