///////////////////////////////////////////////////////////////////// /// Gets the port number for the BSD socket communication. /// /// This method parses the xml file for the socket number. /// \param docname Name of xml file. /// \return the socket port number if successful, or -1 if an error occured. int getsocketportnumber(const char *const docname) { int retVal; char *xPat = "//ipc/socket[@port]"; char *res; int i; res = malloc(BUFFER_LENGTH); if (res == NULL) { perror("malloc failed in getsocketportnumber."); #ifdef NDEBUG fprintf(f1, "malloc failed in getsocketportnumber.\n"); #endif return -1; } if (0 == getxmlvalue(docname, xPat, res, &i, BUFFER_LENGTH)) retVal = atoi((char*)res); else retVal = -1; free(res); return retVal; }
///////////////////////////////////////////////////////////////////// /// Gets the hostname for the BSD socket communication. /// /// This method parses the xml file for the socket host name. /// \param docname Name of xml file. /// \param hostname The hostname will be written to this argument. /// \return 0 if successful, or -1 if an error occured. int getsockethost(const char *const docname, char *const hostname) { char *xPat = "//ipc/socket[@hostname]"; int i; int r = getxmlvalue(docname, xPat, hostname, &i, BUFFER_LENGTH); return r; }
void thread_checkconf() { char file[128]; char cmd[512]; FILE *stream; char md5[35]; char oldmd5[128]={0}; char *p; strcpy( file, "xml.conf" ); pthread_mutex_lock(&mtx); if (!xmlid) resolveconfig( file ); pthread_cond_signal(&cond); pthread_mutex_unlock(&mtx); xmlDocPtr doc = xmlNewDoc(NULL); doc = xmlParseFile("beps.xsd"); char buf[128]; int i=0; sprintf( cmd, "/usr/bin/md5sum %s", file ); while(1) { costt(1,0); stream=popen( cmd, "r" ); if (stream) { p=md5; while( fread( p, sizeof(char), 1, stream ) ) { if (*p==' ') { *p=0; break; } p++; } pclose(stream); if (!*md5) { printf("md5sum err [%s]\n", cmd); continue; } if (strcmp( oldmd5, md5 )) { printf("newmd5=%s\n", md5); resolveconfig( file ); strcpy( oldmd5, md5 ); } } else { printf("%s err!!!!!!\n", cmd); } printf("%lf\n", costt(1,1)); costt(1,0); if(!getxmlvalue( doc, "beps.xsd", "value", i++, buf )) printf("get value err\n"); printf("%s\n", buf); printf("%lf\n", costt(1,1)); sleep(1); } }
int main ( int argc, char *argv[] ) { //定义文档和节点指针 //xmlDocPtr doc = xmlNewDoc(BAD_CAST"1.0"); xmlDocPtr doc = xmlNewDoc(NULL); doc->standalone=-2; xmlNodePtr root_node = xmlNewNode(NULL,BAD_CAST"root"); //设置根节点 xmlDocSetRootElement(doc,root_node); // cur = xmlDocGetRootElement(doc); //获取文档根结点 //在根节点中直接创建节点 xmlNewTextChild(root_node, NULL, BAD_CAST "newNode1", BAD_CAST "newNode1 content"); xmlNewTextChild(root_node, NULL, BAD_CAST "newNode2", BAD_CAST "newNode2 content"); xmlNewTextChild(root_node, NULL, BAD_CAST "newNode3", BAD_CAST "newNode3 content"); /* xmlChar *key; key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); //获取文本结点的文本,需用其子结点 xmlFree(key); */ //创建一个绑定在根节点的子节点 xmlNewChild(root_node, NULL, BAD_CAST "node1",BAD_CAST "content of node1"); /* xmlNodeSetContent(curNode, (xmlChar *) "content changed");//设置结点的文本内容 //得到一个节点的内容: //xmlChar *value = xmlNodeGetContent(node); //返回值value应该使用xmlFree(value)释放内存 xmlUnlinkNode(curNode); //将当前结点从文档中断链(unlink),这样本文档就不会再包含这个子结点 xmlFreeNode(curNode); //手动删除断链结点的内存, 若没有xmlDelNode或者xmlRemoveNode,使用此函数 xmlChar *uri; uri = xmlGetProp(cur, "uri"); //获取属性值 xmlFree(uri); //释放内存 xmlSetProp(curNode,BAD_(xmlChar *)"attribute", (xmlChar *) "no"); //设置当前结点的attribute属性的属性值为no */ //创建一个节点,设置其内容和属性,然后加入根结点 xmlNodePtr node = xmlNewNode(NULL,BAD_CAST"node2"); xmlNodePtr content = xmlNewText(BAD_CAST"NODE CONTENT"); xmlAddChild(root_node,node); xmlAddChild(node,content); xmlNewProp(node,BAD_CAST"attribute",BAD_CAST "yes"); //通过xmlNewProp()增加一个节点的属性 node=xmlNewChild(root_node, NULL, BAD_CAST "node3", BAD_CAST"node has attributes"); xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "no"); //创建一个儿子和孙子节点 node = xmlNewNode(NULL, BAD_CAST "son"); xmlAddChild(root_node,node); xmlNodePtr grandson = xmlNewNode(NULL, BAD_CAST "grandson"); xmlAddChild(node,grandson); xmlAddChild(grandson, xmlNewText(BAD_CAST "This is a grandson node")); //存储xml文档 int nRel = xmlSaveFile("CreatedXml.xml",doc); if (nRel != -1) { printf("%s\n",d_ConvertCharset("GBK", "utf-8", "一个xml文档被创建\n")); } //保存文件 /* * xmlSaveFormatFile (docname, doc, 1); 保存文件到磁盘,第一个参数是写入文件的名,第二个参数是一个xmlDoc结构指针,第三个参数设定为1,保证在输出上写入缩进。 */ xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1); // doc = xmlRecoverMemory(tmpxml,strlen(tmpxml)); // xmlDocDumpMemoryEnc(doc, &buf, &len, "utf-8"); // test_html(); //释放文档内节点动态申请的内存 xmlFreeDoc(doc); /*free the document */ xmlCleanupParser(); xmlMemoryDump();//debug memory for regression tests /* * 加载配置文件 */ pthread_attr_t attr; if(pthread_attr_init(&attr)) { printf("err\n"); return -1; } if(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) { printf("err\n"); return -1; } if( pthread_create( &xmlcfg_tid, &attr, (void *(*)(void *))thread_checkconf, 0 )) printf("create err\n"); pthread_attr_destroy(&attr); pthread_mutex_lock(&mtx); while(!xmlid) pthread_cond_wait(&cond,&mtx); pthread_mutex_unlock(&mtx); /* 注意:xmlReadFile可以控制option */ printf("aaaa\n"); //doc = xmlParseFile("beps.xml"); doc = xmlReadFile("beps.xml","UTF-8", 1); //解析文件 printf("aaaaaaaa\n"); //int fd=open("log", O_WRONLY|O_APPEND|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP ); //ctxt = xmlSaveToFd(1, "UTF-8", -2); xmlSaveCtxtPtr ctxt = xmlSaveToFd(1, NULL, 1|-2); root_node=xmlDocGetRootElement( doc ); //xmlSaveTree(ctxt, root_node); doc->encoding=NULL; xmlSaveDoc(ctxt, doc); xmlSaveClose(ctxt); xmlSaveFormatFileEnc( "-", doc, "UTF-8", 1); printf("aaaaaaaa\n"); //doc = xmlReadFile("beps.xsd","UTF8",XML_PARSE_RECOVER); //解析文件 //检查解析文件是否成功,如果不成功,libxml将指一个注册的错误并停止。 //一个常见错误是不适当的编码。XML标准文件除了用UTF-8或UTF-16外还可用其他编码保存。 //如果文件是这样,libxml将自动地为你转换到UTF-8。更多关于XML编码信息包含在XML标准中. if (NULL == doc) { fprintf(stderr,"Document not parsed successfully.%d\n", __LINE__); return -1; } // printf("xmlDoc name=%s\n",doc->name); // printf("type=%d\n", doc->type); //// printf("oldNs.href=%s\n",doc->oldNs->href); //// printf("oldNs.prefix=%s\n",doc->oldNs->prefix); // printf("URL=%s\n",doc->URL); // printf("children.type=%u\n",doc->children->type); // printf("children.name=%s\n",doc->children->name); // printf("children.content=%s\n",doc->children->content); // if (doc->children->next) // { // printf("children.next.type=%u\n",doc->children->next->type); // printf("children.next.name=%s\n",doc->children->next->name); // } // //printf("children.next.content=%s\n",doc->children->next->content); // //printf("children.last.type=%u\n",doc->children->last->type); // //printf("children.last.name=%s\n",doc->children->last->name); // //printf("children.last.content=%s\n",doc->children->last->content); // root_node=xmlDocGetRootElement( doc ); // // printf("root.type=%u\n", root_node->type); // printf("root.name=%s\n", root_node->name); // printf("root.content=%s\n", root_node->content); // if( root_node->next ) // { // printf("root.next.type=%u\n", root_node->next->type); // printf("root.next.name=%s\n", root_node->next->name); // } // // if( root_node->properties ) // { // printf("root.prop.type=%u\n", root_node->properties->type); // printf("root.prop.name=%s\n", root_node->properties->name); // if (root_node->properties->ns) // { // printf("root.prop.ns.type=%u\n", root_node->properties->ns->type); // printf("root.prop.ns.href=%s\n", root_node->properties->ns->href); // printf("root.prop.ns.prefix=%s\n", root_node->properties->ns->prefix); // printf("root.prop.ns.next.type=%u\n", root_node->properties->ns->next->type); // printf("root.prop.ns.next.href=%s\n", root_node->properties->ns->next->href); // printf("root.prop.ns.next.prefix=%s\n", root_node->properties->ns->next->prefix); // } // } // // if (root_node->ns) // { // printf("root.ns.type=%u\n", root_node->ns->type); // printf("root.ns.href=%s\n", root_node->ns->href); // printf("root.ns.prefix=%s\n", root_node->ns->prefix); // if (root_node->ns->next) // { // printf("root.ns.next.type=%u\n", root_node->ns->next->type); // printf("root.ns.next.href=%s\n", root_node->ns->next->href); // printf("root.ns.next.prefix=%s\n", root_node->ns->next->prefix); // } // } /* -------------------------------------------------------------------------------------------------- XPATH查询函数 -------------------------------------------------------------------------------------------------- l xmlXPathContextPtr context; //XPATH上下文指针 l context = xmlXPathNewContext(doc); //获取context指针 l xmlXPathObjectPtr result;// XPATH对象指针,用来存储查询结果 l result = xmlXPathEvalExpression(xmlChar *xpath, context); //根据条件xpath以及context来进行查询,条件格式:xmlChar *szXpath =(xmlChar *) ("/root/node2[@attribute='yes']"); l xmlXPathFreeContext(context); //释放context内存 l if(xmlXPathNodeSetIsEmpty(result->nodesetval)) //判断查询后的结果是否为空 l xmlNodeSetPtr nodeset; //创建结点集指针 l nodeset = result->nodesetval; //这个结点集对象包含在集合中的元素数目(nodeNr)及一个结点数组(nodeTab)。 l for (i=0; i < nodeset->nodeNr; i++) //遍历结果结点集合 l keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1) l xmlXPathFreeObject (result); //释放内存 l xmlCleanupParser();//清除由libxml2申请的内存 -------------------------------------------------------------------------------------------------- */ // xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); // int status = xmlXPathRegisterNs(xpathCtx,(const xmlChar *)"xs", // (const xmlChar *)"http://www.w3.org/2001/XMLSchema-instance"); // status = xmlXPathRegisterNs(xpathCtx,(const xmlChar *)"ns", // (const xmlChar *)"urn:cnaps:std:beps:2010:tech:xsd:beps.121.001.01"); xmlXPathObjectPtr result; //result=getNodeSet(doc, BAD_CAST"//node2[@attribute='yes']"); //result=getNodeSet(doc, BAD_CAST"/root/*"); //result=getNodeSet(doc, BAD_CAST"//newNode1"); //result=getNodeSet(doc, "/Document/name[@id='1']/xcz"); // result=getNodeSet(doc, "//element[@name='MsgId']"); //result=xmlXPathEvalExpression("//xs:element[@name='MsgId']", xpathCtx); //result=xmlXPathEvalExpression(BAD_CAST"//ns:xcz[position()=2]", xpathCtx); //result=xmlXPathEvalExpression(BAD_CAST"/ns:Document/ns:name[@id='1']/ns:xcz", xpathCtx); //xmlXPathFreeContext(xpathCtx); //释放context内存 // if(xmlXPathNodeSetIsEmpty(result->nodesetval)) //判断查询后的结果是否为空 // { // xmlXPathFreeObject(result); // printf("nodeset is empty\n"); // return 1; // } /* doc = xmlParseFile( FileName); xpathCtx = xmlXPathNewContext( doc); status = xmlXPathRegisterNs( xpathCtx,( const xmlChar *)"abc", ( const xmlChar *)"http://www.abc.org"); */ // printf("type=%d boolval=%d floatval=%lf stringval=%s\n", result->type, result->boolval, // result->floatval, result->stringval); // xmlNodePtr nodet; // int n; // printf("nodeNr=%d nodeMax=%d\n", result->nodesetval->nodeNr, result->nodesetval->nodeMax ); // n=result->nodesetval->nodeNr; int i; // nodet=*result->nodesetval->nodeTab; // while( n-- ) // { // printf("type=[%d]\n", nodet->type); // printf("name=[%s]\n", nodet->name); // //printf("content=[%s]\n", nodet->content); // //printf("ns.href=[%s]ns.prefix=[%s]\n", nodet->ns->href, nodet->ns->prefix ); // printf("attr.name=[%s]\n", nodet->properties->name); // printf("attr.children.content=[%s]\n", nodet->properties->children->content); // //printf("attr._private=[%s]\n", nodet->properties->_private); // //printf("children.content=[%s]\n", nodet->children->content); // nodet=*(result->nodesetval->nodeTab+i++); // } // xmlNodePtr cur; // xmlChar *value; // for (i=0; i < result->nodesetval->nodeNr; i++) // { // cur = result->nodesetval->nodeTab[i]; // // cur = cur->xmlChildrenNode; // if(cur!=NULL) // { // value = xmlGetProp(cur,(const xmlChar *)"name"); // if (value&&value[0]) // { // printf("value: %s\n\n", d_ConvertCharset("utf-8", "GBK", (char *)value)); // xmlFree(value); // } // value = xmlNodeGetContent(cur); // if (value&&value[0]) // { // printf("value: %s\n\n", d_ConvertCharset("utf-8", "GBK", (char *)value)); // xmlFree(value); // } // } // } //doc = xmlNewDoc(BAD_CAST"1.0"); //printf("%d\n", __LINE__); //xmlDocSetRootElement(doc, *result->nodesetval->nodeTab); //printf("%d\n", __LINE__); // xmlSaveFile("1CreatedXml.xml",doc); // xml_tranid *xp; // xml_element *ep; // xp=xmlid; /* * 还需要考虑resolveconfig如何触发 可以起单独线程 间隔性检查文件修改时间 * 另外修改时加读写锁 * 遍历xmlid结构的方法 注意加读锁 */ // for( i=0; i<xmlnum; i++ ) // { // printf("tranid=[%s]\n", xmlid[i].tranid); // ep=xmlid[i].ele; // while( ep ) // { // printf("name=[%s]\n", ep->name); // printf("path=[%s]\n", ep->path); // printf("type=[%s]\n", ep->type); // printf("prop=[%s]\n", ep->prop); // ep=ep->next; // } // } /* int fd; fd=open("log", O_WRONLY|O_APPEND|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP ); if (fd==-1) { printf("open log err\n"); } if (dup2(fd, STDOUT_FILENO)!=STDOUT_FILENO) { printf("dup stdout err\n"); } */ char buf[128]; if(!getxmlvalue( doc, "1111", "id1", 0, buf )) printf("get value err\n"); printf("%s\n", buf); if(!getxmlvalue( doc, "1111", "nameid", 0, buf )) printf("get value err\n"); printf("%s\n", buf); xmlFreeDoc(doc); /*free the document */ xmlCleanupParser(); xmlMemoryDump();//debug memory for regression tests while(1) { costt(1,0); doc = xmlParseFile("beps.xml"); if(!getxmlvalue( doc, "1111", "count", 0, &i )) printf("get value err\n"); printf("%d\n", i); xmlFreeDoc(doc); /*free the document */ xmlCleanupParser(); xmlMemoryDump();//debug memory for regression tests printf("%lf\n", costt(1,1)); sleep(1); } // xmlXPathFreeObject (result); return EXIT_SUCCESS; } /* ---------- end of function main ---------- */