xmlDocPtr doc = xmlNewDoc(BAD_CAST"1.0"); xmlNodePtr root_node = xmlNewNode(NULL,BAD_CAST"root"); //设置根节点 xmlDocSetRootElement(doc,root_node); //在根节点中直接创建节点 // 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"); //创建一个节点,设置其内容和属性,然后加入根结点 // 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"); //创建一个儿子和孙子节点 xmlNodePtr node = xmlNewNode(NULL, BAD_CAST "son1"); xmlAddChild(root_node,node); xmlNodePtr grandson = xmlNewNode(NULL, BAD_CAST "om_alarm_id"); xmlAddChild(node,grandson); xmlAddChild(grandson, xmlNewText(BAD_CAST "3")); grandson = xmlNewNode(NULL, BAD_CAST "alarm_position"); xmlAddChild(node,grandson); xmlAddChild(grandson, xmlNewText(BAD_CAST "E:\\project\\test")); grandson = xmlNewNode(NULL, BAD_CAST "alarm_type"); xmlAddChild(node,grandson); xmlAddChild(grandson, xmlNewText(BAD_CAST "5")); node = xmlNewNode(NULL, BAD_CAST "son2"); xmlAddChild(root_node,node); grandson = xmlNewNode(NULL, BAD_CAST "om_alarm_id"); xmlAddChild(node,grandson); xmlAddChild(grandson, xmlNewText(BAD_CAST "4")); grandson = xmlNewNode(NULL, BAD_CAST "alarm_position"); xmlAddChild(node,grandson); xmlAddChild(grandson, xmlNewText(BAD_CAST "E:\\project\\test\\libxml")); grandson = xmlNewNode(NULL, BAD_CAST "alarm_type"); xmlAddChild(node,grandson); xmlAddChild(grandson, xmlNewText(BAD_CAST "6")); node = xmlNewNode(NULL, BAD_CAST "son3"); xmlAddChild(root_node,node); grandson = xmlNewNode(NULL, BAD_CAST "om_alarm_id"); xmlAddChild(node,grandson); xmlAddChild(grandson, xmlNewText(BAD_CAST "5")); grandson = xmlNewNode(NULL, BAD_CAST "alarm_position"); xmlAddChild(node,grandson); xmlAddChild(grandson, xmlNewText(BAD_CAST "E:\\project\\test\\mcfddfa")); grandson = xmlNewNode(NULL, BAD_CAST "alarm_type"); xmlAddChild(node,grandson); xmlAddChild(grandson, xmlNewText(BAD_CAST "1")); //存储xml文档 int nRel = xmlSaveFile("CreatedXml.xml",doc); if (nRel != -1) { // MessageBox("一个xml文档被创建,写入个字节"); } //释放文档内节点动态申请的内存 xmlFreeDoc(doc);
解析
xmlDocPtr doc; //定义解析文档指针 xmlNodePtr curNode; //定义结点指针(你需要它为了在各个结点间移动) xmlChar *szKey; //临时字符串变量 char *szDocName; char error[256]=""; szDocName = "./CreatedXml.xml"; doc = xmlReadFile(szDocName,"UTF-8",XML_PARSE_RECOVER); //解析文件 //检查解析文档是否成功,如果不成功,libxml将指一个注册的错误并停止。 //一个常见错误是不适当的编码。XML标准文档除了用UTF-8或UTF-16外还可用其它编码保存。 //如果文档是这样,libxml将自动地为你转换到UTF-8。更多关于XML编码信息包含在XML标准中. if (NULL == doc) { MessageBox("error"); } curNode = xmlDocGetRootElement(doc); //确定文档根元素 if (NULL == curNode) { MessageBox("error"); xmlFreeDoc(doc); } if (xmlStrcmp(curNode->name, BAD_CAST "root")) { MessageBox("error"); xmlFreeDoc(doc); } curNode = curNode->xmlChildrenNode; xmlNodePtr propNodePtr; while(curNode != NULL) { //取出节点中的内容 memset(&alarm_reg,0x00,sizeof(alarm_reg)); propNodePtr = curNode->xmlChildrenNode; if ((!xmlStrcmp(curNode->name, (const xmlChar *)"son"))) { while(propNodePtr!=NULL) { if((!xmlStrcmp(propNodePtr->name,(const xmlChar *)"om_alarm_id"))) { szKey = xmlNodeGetContent(propNodePtr); // sprintf(alarm_reg.alarm_id,"%s",(char*)szKey); sscanf((char*)szKey,"%s",alarm_reg.alarm_id); sprintf(error,"%s",alarm_reg.alarm_id); WRITE_LOG(error,LOG_TYPE_INFO,LOG_LEVEL_RUN,true); xmlFree(szKey); } if((!xmlStrcmp(propNodePtr->name,(const xmlChar *)"alarm_position"))) { szKey = xmlNodeGetContent(propNodePtr); sscanf((char*)szKey,"%s",alarm_reg.alarm_picpath); sprintf(error,"%s",alarm_reg.alarm_picpath); WRITE_LOG(error,LOG_TYPE_INFO,LOG_LEVEL_RUN,true); //WRITE_LOG((char*)szKey,LOG_TYPE_INFO,LOG_LEVEL_RUN,true); xmlFree(szKey); } if((!xmlStrcmp(propNodePtr->name,(const xmlChar *)"alarm_type"))) { szKey = xmlNodeGetContent(propNodePtr); sscanf((char*)szKey,"%d",&alarm_reg.alarm_type); sprintf(error,"%d",alarm_reg.alarm_type); WRITE_LOG(error,LOG_TYPE_INFO,LOG_LEVEL_RUN,true); xmlFree(szKey); } propNodePtr=propNodePtr->next; } //szKey = xmlNodeGetContent(curNode); } //查找带有属性attribute的节点 /* if (xmlHasProp(curNode,BAD_CAST "attribute")) { propNodePtr = curNode; } */ curNode = curNode->next; } //查找属性 /* xmlAttrPtr attrPtr = propNodePtr->properties; while (attrPtr != NULL) { if (!xmlStrcmp(attrPtr->name, BAD_CAST "attribute")) { xmlChar* szAttr = xmlGetProp(propNodePtr,BAD_CAST "attribute"); cout<<"get attribute = "<<szAttr<<endl; xmlFree(szAttr); } attrPtr = attrPtr->next; } */ xmlFreeDoc(doc);
typedef struct DB_REG_ALARM_INFO_ { unsigned char alarm_id[64]; unsigned int alarm_type; //报警类型 unsigned char alarm_picpath[256];//报警图片的位置 }DB_REG_ALARM_INFO,*DB_REG_ALARM_INFO_PTR;