使用libxml2创建和解析xml文件

毕业设计需要用到xml文件来组织和存放数据,

对于Linux环境下,有libxml2可供使用。

 

经过一段时间查询文档和网站,

基本掌握创建xml文档和解析xml的操作,

简单做一下记录。

 

 

创建xml

例子如下:

 

 1 #include <stdio.h>

 2 #include <libxml/parser.h>

 3 #include <libxml/tree.h>

 4 

 5 int main(int argc, char **argv)

 6 {

 7         xmlDocPtr doc = NULL;

 8         xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;

 9 

10         doc = xmlNewDoc(BAD_CAST "1.0"); // create a new xml document.

11         root_node = xmlNewNode(NULL, BAD_CAST "root"); // create a root node.

12         xmlDocSetRootElement(doc, root_node);

13 

14         xmlNewChild(root_node, NULL, BAD_CAST "node1", BAD_CAST "content of node1");

15         //xmlNewChild(root_node, NULL, BAD_CAST "node2", NULL);

16 

17         node = xmlNewChild(root_node, NULL, BAD_CAST "node3", BAD_CAST "node3 has attributes");

18         xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");

19 

20         node = xmlNewNode(NULL, BAD_CAST "node4");

21         node1 = xmlNewText(BAD_CAST

22                    "other way to create content (which is also a node)");

23         xmlAddChild(node, node1);

24         xmlAddChild(root_node, node);

25 

26         xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);

27 

28         xmlFreeDoc(doc);

29 

30         xmlCleanupParser();

31 

32         xmlMemoryDump();

33         return(0);

34 }
View Code

 libxml的api使用 const unsigned char* 。

而string literal 只能隐式转换到 const char*。

所以libxml提供一个BAD_CAST用来作显示转换。

 

代码应该不难看懂,生成的xml文件如下:

1 <?xml version="1.0" encoding="UTF-8"?>

2 <root>

3   <node1>content of node1</node1>

4   <node3 attribute="yes">node3 has attributes</node3>

5   <node4>other way to create content (which is also a node)</node4>

6 </root>
View Code

xml文件和创建xml的代码对照着看就很容易看懂如何生成节点以及属性了。

 

 

解析xml

代码如下:

 1 #include <stdio.h>

 2 #include <stdlib.h>

 3 #include <string.h>

 4 #include <libxml/parser.h>

 5 #include <libxml/xmlmemory.h>

 6 

 7 int main(int argc, char **argv)

 8 {

 9         xmlDocPtr doc;

10         xmlNodePtr curNode;

11 

12         xmlKeepBlanksDefault(0);

13         doc = xmlReadFile("mine.xml", "UTF-8", XML_PARSE_RECOVER); // open a xml doc.

14         curNode = xmlDocGetRootElement(doc); // get root element.

15 

16         if (curNode == NULL)

17         {

18                 fprintf(stderr, "open file failed. \n");

19                 xmlFreeDoc (doc);

20                 return -1;

21         }

22 

23         if (xmlStrcmp(curNode->name, "root")) // if the same,xmlStrcmp return 0, else return 1

24         {

25                 fprintf(stderr, "check rootElement failed. \n");

26                 xmlFreeDoc (doc);

27                 return -1;

28         }

29 

30         curNode = curNode->children; // move to root element's children.

31         char *nodeName;

32         char *content;

33 

34         if (curNode != NULL)

35         {

36                 nodeName = (char *) curNode->name; 

37                 content = (char *) xmlNodeGetContent(curNode);

38                 printf ("Current node name:%s,\t the content is:%s.\n\n", nodeName, content);

39         }

40 

41         curNode = curNode->next;

42         char *attr;

43         if (curNode != NULL)

44         {

45                 nodeName = (char *) curNode->name;

46                 content = (char *) xmlNodeGetContent(curNode);

47                 attr = (char *) xmlGetProp(curNode, (const xmlChar *)"attribute"); // get node attribute

48                 printf ("Current node name:%s,\t the content is:%s,\t AND THE ATTR IS:%s.\n\n", nodeName, content,attr); 

49         }

50 

51         curNode = curNode->next;

52         if (curNode != NULL)

53         {

54                 nodeName = (char *) curNode->name;

55                 content = (char *) xmlNodeGetContent(curNode);

56                 printf ("Current node name:%s,\t the content is:%s.\n\n", nodeName, content);   

57         }

58 

59         xmlFree(curNode);

60         xmlFreeDoc(doc);

61         return 1;

62 }
View Code

 

上面的代码是简单的按生成的xml结构来解析,

正确的用法应该是写成一个函数来调用,

可以解析任何的已知根节点的xml文件。

 

解析的结果输入如下:

1 [nigelzeng@ubuntu xml-learning]$ ./xml-mine-parse 

2 Current node name:node1,         the content is:content of node1.

3 

4 Current node name:node3,         the content is:node3 has attributes,    AND THE ATTR IS:yes.

5 

6 Current node name:node4,         the content is:other way to create content (which is also a node).
View Code

 

 

参考:

http://xmlsoft.org/index.html

http://www.cppblog.com/lymons/archive/2009/03/30/37553.html

http://www.4ucode.com/Study/Topic/1622022

你可能感兴趣的:(解析xml)