linux xml解析
来源:互联网 发布:txt阅读软件下载 编辑:程序博客网 时间:2024/06/06 07:31
废话不多话,直接上源码!
<?xml version="1.0"?>
<carport name="bochiot company">
<carport_info name="carport_name">
<carport_x>100</carport_x>
<carport_y>200</carport_y>
<zone_num>2</zone_num>
</carport_info>
<zone_group name="zone_group">
<zone id="101" name="zone1">
<coordinate>
<coor_x>1</coor_x>
<coor_y>2</coor_y>
<bind_device_mac type="1">AAAAAAAAAAAA</bind_device_mac>
</coordinate>
<coordinate>
<coor_x>3</coor_x>
<coor_y>4</coor_y>
<bind_device_mac type="0">BBBBBBBBBBBB</bind_device_mac>
</coordinate>
<coordinate>
<coor_x>5</coor_x>
<coor_y>6</coor_y>
</coordinate>
</zone>
<zone id="102" name="zone2">
<coordinate>
<coor_x>7</coor_x>
<coor_y>8</coor_y>
<bind_device_mac type="1">CCCCCCCCCCCC</bind_device_mac>
</coordinate>
<coordinate>
<coor_x>9</coor_x>
<coor_y>10</coor_y>
<bind_device_mac type="0">DDDDDDDDDDDD</bind_device_mac>
</coordinate>
<coordinate>
<coor_x>1</coor_x>
<coor_y>5</coor_y>
</coordinate>
</zone>
</zone_group>
</carport>
--------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
int GetCurFilePath(char *lpOut) // get full path of the executable file
{
char chPath[BUFSIZ] = {0};
int nRetVal = readlink("/proc/self/exe", chPath, sizeof(chPath)); // get full path of the current-executable file
if(nRetVal < 0)
{
strcpy(lpOut, ".");
return -1;
}
else
{
strcpy(lpOut, chPath);
return 0;
}
}
int GetCurDir(char *lpOut) // get directory-path of current executable-file
{
char chPath[BUFSIZ] = { 0 };
if( GetCurFilePath(chPath) < 0 )
return - 1;
dirname(chPath); // dirname will change value of "chPath"(contain result)
strcpy(lpOut, chPath); // copy result to out-param
return 0;
}
xmlDocPtr getdoc(char *docname) // 根据文件名得到文档指针
{
xmlDocPtr doc;
xmlKeepBlanksDefault(0);
doc = xmlParseFile(docname);
if(doc == NULL)
{
fprintf(stderr, "Document not parsed successfully.\n");
return NULL;
}
return doc;
}
// 在文档doc中解析xpath表达式,返回结果集指针
xmlXPathObjectPtr getnodeset(xmlDocPtr doc, xmlChar *xpath)
{
xmlXPathContextPtr context;
xmlXPathObjectPtr result;
context = xmlXPathNewContext(doc);
if(context == NULL)
{
printf("Error in xmlXPathNewContent\n");
return NULL;
}
result = xmlXPathEvalExpression(xpath, context); // 在context中解析表达式xpath
xmlXPathFreeContext(context); // 释放context
if(result == NULL)
{
printf("Error in xmlXPathEvalExpression\n");
return NULL;
}
if(xmlXPathNodeSetIsEmpty(result->nodesetval)) // 解析表达式的结果集为空
{
xmlXPathFreeObject(result);
printf("No result\n");
return NULL;
}
return result;
}
void CoorReadXmlDoc(xmlDocPtr doc, xmlNodePtr cur)
{
xmlChar *key,*type;
cur = cur->xmlChildrenNode;
while (cur != NULL)
{
if(xmlStrcmp(cur->name, (const xmlChar*) "coor_x")==0)
{
printf("cur->name = %s\t", cur->name);
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf(" key=%s\n", (char*)key);
}
else if(xmlStrcmp(cur->name, (const xmlChar*) "coor_y")==0)
{
printf("cur->name = %s\t", cur->name);
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf(" key=%s\n", (char*)key);
}
else if(xmlStrcmp(cur->name, (const xmlChar*) "bind_device_mac")==0)
{
printf("cur->name = %s\t", cur->name);
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
type = xmlGetProp(cur, (const xmlChar*) "type"); // 获取属性值
printf(" type=%s\t", type);
printf(" key=%s\n", (char*)key);
}
else
{
printf("CoorReadXmlDoc error=%s!\n",cur->name);
}
cur = cur->next;
}
}
void ZoneReadXmlDoc(xmlDocPtr doc, xmlNodePtr cur)
{
xmlChar *key,*type;
cur = cur->xmlChildrenNode;
while (cur != NULL)
{
if(xmlStrcmp(cur->name, (const xmlChar*) "coordinate")==0)
{
printf("cur->name = %s\n", cur->name);
CoorReadXmlDoc(doc,cur);
}
else
{
printf("ZoneReadXmlDoc error=%s!\n",cur->name);
}
cur = cur->next;
}
}
// 解析xmlPath路径的结点
void ReadXmlDoc(char *filepath, char *xmlPath)
{
xmlDocPtr doc = getdoc(filepath);
if(NULL == doc)
return ;
int i;
xmlChar *key;
xmlChar *xpath = (xmlChar*) xmlPath;
xmlXPathObjectPtr result = getnodeset(doc, xpath); // 获取结果集
if(result)
{
xmlNodeSetPtr nodeset = result->nodesetval;
xmlChar *name, *value,*type;
printf("nodeset->nodeNr = %d\n", nodeset->nodeNr); // 打印结果集中结点个数
for(i= 0; i < nodeset->nodeNr; i++)
{
xmlNodePtr cur = nodeset->nodeTab[i]; // products
name = xmlGetProp(cur, (const xmlChar*) "name");// 获取属性值
printf("cur->name@ = %s, name = %s\n", cur->name,name);
cur = cur->xmlChildrenNode;
while(cur)
{
if(xmlStrcmp(cur->name, (const xmlChar*) "carport_info")==0)
{
printf("cur->name = %s\n", cur->name);
ReadXmlDoc(filepath, "/carport/carport_info");
}
else if(xmlStrcmp(cur->name, (const xmlChar*) "carport_x")==0)
{
printf("cur->name = %s\t", cur->name);
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf(" key=%s\n", (char*)key);
}
else if(xmlStrcmp(cur->name, (const xmlChar*) "carport_y")==0)
{
printf("cur->name = %s\t", cur->name);
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf(" key=%s\n", (char*)key);
}
else if(xmlStrcmp(cur->name, (const xmlChar*) "zone_num")==0)
{
printf("cur->name = %s\t", cur->name);
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf(" key=%s\n", (char*)key);
printf("-----------------\n");
}
//////////////////////////////////////////////
else if(xmlStrcmp(cur->name, (const xmlChar*) "zone_group")==0) // cur->name不为"text"
{
printf("cur->name = %s\n", cur->name);
ReadXmlDoc(filepath, "/carport/zone_group");
}
else if(xmlStrcmp(cur->name, (const xmlChar*) "zone")==0)
{
printf("cur->name = %s\t", cur->name);
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
name = xmlGetProp(cur, (const xmlChar*) "name");// 获取属性值
printf(" name=%s\n", name);
//CoorReadXmlDoc(filepath, "/carport/zone_group/zone/coordinate");
ZoneReadXmlDoc(doc,cur);
printf("***************\n");
}
cur = cur->next;
}
printf("\n");
}
xmlXPathFreeObject(result);
}
xmlFreeDoc(doc);
xmlCleanupParser();
}
int main(void)
{
char curDir[100] = {0};
char docname[100] = {0};
GetCurDir(curDir);
strcpy(docname, curDir);
strcat(docname, "/CreatedXml.xml");
ReadXmlDoc(docname, "/carport");
return EXIT_SUCCESS;
}
搞了一天终于把XML的多级解析搞定,心酸!
- Linux C解析xml
- linux xml解析
- linux下 libxml2 xml解析
- Linux 下配置Expat解析XML
- linux shell 解析xml格式文本
- [Linux C]利用libxml2解析xml文件
- [Linux C]利用libxml2解析xml文件
- Linux下的xml文件的解析
- linux下用库libxml2解析xml文件
- Linux下使用libxml2解析XML配置文件
- Linux 下C/C++解析XML文件
- Linux下XPath对xml解析
- 嵌入式Linux解析XML开发之:(一)XML基础
- XML解析
- xml解析
- XML解析
- 解析XML
- xml解析
- COCI CONTEST #3 29.11.2014 STOGOVI
- ios开发出现 -[__NSDictionaryM isEqualToString:]: unrecognized selector sent to instance 错误
- 1236 -- 别撞坏了
- ARC下强引用弱引用retainCount增减规律
- 我的开山之语!
- linux xml解析
- HTML5 postMessage 和 onmessage API 详细应用
- 第二周项目1-c/c++语言中函数参数传递的三种方式
- 关于Jetty锁定静态文件的问题解决办法
- 1237 -- 地盘划分
- response.setContentType()中MIME参数类型总结
- 字符串处理函数——提取字符串中的字符(重要)
- 求串联电阻值
- Glide :一个android平台上的快速和高效的开源的多媒体资源管理库, 提供 多媒体文件的压缩,内存和磁盘缓存, 资源池的接口