1、在Linux下采用libxml2解析xml文件,先通过终端对libxml2库函数进行安装,在终端输入:
sudo apt-get install libxml2-dev
安装完后,会在 “/usr/include”中产生一个“libxml2/libxml”的文件夹,头文件都在这个文件夹里面,但是这里面的头文件在包含其他的头文件时路径是“#include <libxml/*.h>”,需要自己进行手懂得修改,可以重新将头文件按“/usr/include/libxml”来存放,或者是逐个修改为“#include <libxml2/libxml/*.h>”。 也可以再终端通过输入:
dpkg -L libxml2-dev 或者 xml2-config --cflags
来查看libxml2库函数是否安装成功。 参考:https://www.linuxidc.com/Linux/2014-05/101925.htm
2、创建一个简单的xml文件,并内容写入xml文件中:
#include <stdio.h>
#include <libxml2/libxml/parser.h>
#include <libxml2/libxml/tree.h>
int main(int argc, char **argv)
{
xmlDocPtr doc = NULL; /* document pointer */
xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;/* node pointers */
// Creates a new document, a node and set it as a root node
doc = xmlNewDoc(BAD_CAST "1.0");
root_node = xmlNewNode(NULL, BAD_CAST "root");
xmlDocSetRootElement(doc, root_node);
//creates a new node, which is "attached" as child node of root_node node.
xmlNewChild(root_node, NULL, BAD_CAST "node1",BAD_CAST "content of node1");
// xmlNewProp() creates attributes, which is "attached" to an node.
node=xmlNewChild(root_node, NULL, BAD_CAST "node3", BAD_CAST"node has attributes");
xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");
//Here goes another way to create nodes.
node = xmlNewNode(NULL, BAD_CAST "node4");
node1 = xmlNewText(BAD_CAST"other way to create content");
xmlAddChild(node, node1);
xmlAddChild(root_node, node);
//Dumping document to stdin or file
xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
/*free the document */
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();//debug memory for regression tests
return 0;
}
编译时加上 “ -lxml2 ”,然后运行生成的可执行文件,如果没加上文件名则在终端里显示如下信息:
,
如果加上文件名则生成相对应的文件,并将内容以xml的格式写入,例如:
3、从xml文件中解析出数据,并在终端中显示出来:
#include <stdio.h>
#include <libxml2/libxml/parser.h>
#include <libxml2/libxml/tree.h>
int main(int argc, char** argv)
{
xmlDocPtr doc=NULL;
xmlNodePtr cur=NULL;
char* name=NULL;
char* value=NULL;
xmlKeepBlanksDefault (0);
if(argc<2)
{
printf("ERROR: argc must be 2 or above.\n");
return -1;
}
//create Dom tree
doc=xmlParseFile(argv[1]);
if(doc==NULL)
{
printf("ERROR: Loading xml file failed.\n");
exit(1);
}
// get root node
cur=xmlDocGetRootElement(doc);
if(cur==NULL)
{
printf("ERROR: empty file\n");
xmlFreeDoc(doc);
exit(2);
}
//walk the tree
cur=cur->xmlChildrenNode; //get sub node
while(cur !=NULL)
{
name=(char*)(cur->name);
value=(char*)xmlNodeGetContent(cur);
printf("DEBUG: name is: %s, value is: %s\n", name, value);
xmlFree(value);
xmlChar* szAttr = xmlGetProp(cur,BAD_CAST "attribute");
printf("DEBUG: attribute is: %s\n", (char *)szAttr );
xmlFree(szAttr);
cur=cur->next;
}
// release resource of xml parser in libxml2
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}
在编译时加入“ -lxml2 ” ,如下:
4、对xml文件中的数据进行修改:
#include <stdio.h>
#include <libxml2/libxml/parser.h>
#include <libxml2/libxml/tree.h>
int main(int argc, char** argv)
{
xmlDocPtr doc=NULL;
xmlNodePtr cur=NULL;
xmlNodePtr ptr=NULL;
char* name=NULL;
char* value=NULL;
xmlKeepBlanksDefault (0); //去除空行
if(argc<2)
{
printf("ERROR: argc must be 2 or above.\n");
return -1;
}
//create Dom tree
doc=xmlParseFile(argv[1]);
if(doc==NULL)
{
printf("ERROR: Loading xml file failed.\n");
exit(1);
}
// get root node
cur=xmlDocGetRootElement(doc);
if(cur==NULL)
{
printf("ERROR: empty file\n");
xmlFreeDoc(doc);
exit(2);
}
//walk the tree
cur=cur->xmlChildrenNode; //get sub node
ptr=cur->next;
xmlUnlinkNode(cur);
xmlFreeNode(cur);
cur=ptr;
cur=cur->next;
//修改节点的属性值
xmlSetProp(cur,BAD_CAST "attribute", BAD_CAST "no");
//修改节点的内容
xmlNodeSetContent(cur, BAD_CAST "content changed");
xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
// release resource of xml parser in libxml2
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}
如上编译后,运行结果如下图所示:
对比之前的文件内容,可以发现文件内容被改变,之前文件内容如下:
以上程序参考至:http://blog.chinaunix.net/uid-25695950-id-3921541.html