读书人

应用libxml2创建和解析xml文件

发布时间: 2012-09-02 21:00:34 作者: rapoo

使用libxml2创建和解析xml文件

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

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

?

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

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

简单做一下记录。

?

?

创建xml

例子如下:

?

#include <stdio.h>#include <libxml/parser.h>#include <libxml/tree.h>int main(int argc, char **argv){        xmlDocPtr doc = NULL;        xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;        doc = xmlNewDoc(BAD_CAST "1.0"); // create a new xml document.        root_node = xmlNewNode(NULL, BAD_CAST "root"); // create a root node.        xmlDocSetRootElement(doc, root_node);        xmlNewChild(root_node, NULL, BAD_CAST "node1", BAD_CAST "content of node1");        //xmlNewChild(root_node, NULL, BAD_CAST "node2", NULL);        node = xmlNewChild(root_node, NULL, BAD_CAST "node3", BAD_CAST "node3 has attributes");        xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");        node = xmlNewNode(NULL, BAD_CAST "node4");        node1 = xmlNewText(BAD_CAST                   "other way to create content (which is also a node)");        xmlAddChild(node, node1);        xmlAddChild(root_node, node);        xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);        xmlFreeDoc(doc);        xmlCleanupParser();        xmlMemoryDump();        return(0);}

?libxml的api使用 const unsigned char* 。

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

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

?

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

?

<?xml version="1.0" encoding="UTF-8"?><root>  <node1>content of node1</node1>  <node3 attribute="yes">node3 has attributes</node3>  <node4>other way to create content (which is also a node)</node4></root>

?

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

?

?

解析xml

代码如下:

?

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <libxml/parser.h>#include <libxml/xmlmemory.h>int main(int argc, char **argv){        xmlDocPtr doc;        xmlNodePtr curNode;        xmlKeepBlanksDefault(0);        doc = xmlReadFile("mine.xml", "UTF-8", XML_PARSE_RECOVER); // open a xml doc.        curNode = xmlDocGetRootElement(doc); // get root element.        if (curNode == NULL)        {                fprintf(stderr, "open file failed. \n");                xmlFreeDoc (doc);                return -1;        }        if (xmlStrcmp(curNode->name, "root")) // if the same,xmlStrcmp return 0, else return 1        {                fprintf(stderr, "check rootElement failed. \n");                xmlFreeDoc (doc);                return -1;        }        curNode = curNode->children; // move to root element's children.        char *nodeName;        char *content;        if (curNode != NULL)        {                nodeName = (char *) curNode->name;                 content = (char *) xmlNodeGetContent(curNode);                printf ("Current node name:%s,\t the content is:%s.\n\n", nodeName, content);        }        curNode = curNode->next;        char *attr;        if (curNode != NULL)        {                nodeName = (char *) curNode->name;                content = (char *) xmlNodeGetContent(curNode);                attr = (char *) xmlGetProp(curNode, (const xmlChar *)"attribute"); // get node attribute                printf ("Current node name:%s,\t the content is:%s,\t AND THE ATTR IS:%s.\n\n", nodeName, content,attr);         }        curNode = curNode->next;        if (curNode != NULL)        {                nodeName = (char *) curNode->name;                content = (char *) xmlNodeGetContent(curNode);                printf ("Current node name:%s,\t the content is:%s.\n\n", nodeName, content);           }        xmlFree(curNode);        xmlFreeDoc(doc);        return 1;}

?

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

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

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

?

解析的结果输入如下:

?

[nigelzeng@ubuntu xml-learning]$ ./xml-mine-parse Current node name:node1,         the content is:content of node1.Current node name:node3,         the content is:node3 has attributes,    AND THE ATTR IS:yes.Current node name:node4,         the content is:other way to create content (which is also a node).

?

?

参考:

http://xmlsoft.org/index.html

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

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

?

读书人网 >XML SOAP

热点推荐