简单的libxml2使用实例
XML--Libxml2使用实例
一,使用Libxml2生成xml
1,编辑生成
#include <stdio.h>
#include <libxml/parser.h>
#include <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 stdio 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);
}2,编译运行
g++ xmlCreator.cpp -o xmlCreator -I /home/xb/libxml2/xmlinst/include/libxml2/ -L /home/xb/libxml2/xmlinst/lib/ -lxml2 (绿色文字为libxml2安装路径)
3,生成的xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<node1>content of node 1</node1>
<node3 attribute="yes">node has attributes</node3>
<node4>other way to create content</node4>
</root>
两个实例,说明如何使用Libxml2遍历xml文档和使Xpath获取特定结点的内容值:(程序使用的xml文档为
<?xml version="1.0" encoding="UTF-8"?>
<root>
<node1>content of node 1</node1>
<node3 attribute="yes">node has attributes</node3>
<node4>other way to create ontent</node4>
</root>)
二,遍历程序代
1,代码
#include <iostream>
#include <libxml/parser.h>
#include <libxml/tree.h>
using namespace std;
int main(int argc,char** argv)
{ xmlDocPtr doc=NULL;
xmlNodePtr cur=NULL;
char* name=NULL;
char* value=NULL;
xmlKeepBlanksDefault (0);
if(argc<2)
{
cout<<"argc must be 2 or above."<<endl;
return -1;
}
doc=xmlParseFile(argv[1]);//创建Dom树
if(doc==NULL)
{
cout<<"Loading xml file failed."<<endl;
exit(1);
}
cur=xmlDocGetRootElement(doc);//获取根节点
if(cur==NULL)
{
cout<<"empty file"<<endl;
xmlFreeDoc(doc);
exit(2);
}
//walk the tree
cur=cur->xmlChildrenNode;//get sub node
while(cur !=NULL)
{
name=(char*)(cur->name);
value=(char*)xmlNodeGetContent(cur);
cout<<"name is: "<<name<<", value is: "<<value<<endl;
xmlFree(value);
cur=cur->next;
}
xmlFreeDoc(doc);//释放xml解析库所用资源
xmlCleanupParser();
return 0;
}
2,说明:
1
libxml2使用样例_IT/计算机_专业资料。这里主要讲述 libxml2 在 linux 下的... xmlFreeDoc(doc); return -1; } /*在这个例子中,我们需要确认文档是正确的...
libxml2实例详解_IT/计算机_专业资料。libxml2实例详解libxml2 实例详解 实例详解最近因为资源区的一个天气预报的项目需要使用 libxml2 库来解析合作方提供的 xml ...
libxml2实例详解 7页 1下载券 libxml2在linux下的使用 26页 1下载券 使用libxml2生成xml 4页 免费libxml2使用指...
使用Libxml2操作XML文档 Libxml2 是一个xml的c语言版的解析器,本来是为Gnome...示例6: xmlChar *ConvertInput(const char *in, const char *encoding) { ...
Libxml2使用实例 3页 1下载券 使用libxml2生成xml 4页 免费 喜欢此文档的...如果你的数据使用其它编码,它 必须被转换到 UTF-8 才能使用 libxml 的函数。 ...
读取节点信息 Libxml2使用一个树型结构组织XML文件的全部信息,包括节点的子节点、属性等,则可以通过直接使用节点指针读取信息,但不是个通用方式。使用xmlReader和xml...
使用libxml2生成xml_IT/计算机_专业资料。使用 libxml2 生成 xml 文档 2009-... libxml2在linux下的使用 26页 1下载券 libxml2应用实例 19页 免费 libxml2...
基于以上几点原因,决定写一个在Windows平台下,使用C/C++语言,应用LibXml2库来... xmlFreeDoc(doc); return -1; } /*在这个例子中,我们需要确认文档是正确的...
要使用自动通知,需要在 automaticallyNotifiesObserversForKey 方法中明确告诉cocoa,... Libxml2使用实例 6页 1下载券 libxml2他人笔记 6页 2下载券 Libxml2使用...
思维导图使用实例_计算机软件及应用_IT/计算机_专业资料 暂无评价
热门文档