XML_CPP_资料_libXml2_01_Code
ZC: 这里的代码,就是 http://www.cnblogs.com/cppskill/p/6207609.html(我的文章"XML_CPP_资料_libXml2_01 - CppSkill - 博客园.html")里面的代码...
1、创建xml文档
1.1、CreateXmlFile.cpp
/********************************************************************
created: 2007/11/09
created: 9:11:2007 15:34
filename: CreateXmlFile.cpp
author: Wang xuebin
depend: libxml2.lib
build: nmake TARGET_NAME=CreateXmlFile purpose: 创建一个xml文件
*********************************************************************/ #include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <iostream.h> int main()
{
//定义文档和节点指针
xmlDocPtr doc = xmlNewDoc(BAD_CAST"1.0");
xmlNodePtr root_node = xmlNewNode(NULL,BAD_CAST"root"); //设置根节点
xmlDocSetRootElement(doc,root_node); //在根节点中直接创建节点
xmlNewTextChild(root_node, NULL, BAD_CAST "newNode1", BAD_CAST "newNode1 content");
xmlNewTextChild(root_node, NULL, BAD_CAST "newNode2", BAD_CAST "newNode2 content");
xmlNewTextChild(root_node, NULL, BAD_CAST "newNode3", BAD_CAST "newNode3 content"); //创建一个节点,设置其内容和属性,然后加入根结点
xmlNodePtr node = xmlNewNode(NULL,BAD_CAST"node2");
xmlNodePtr content = xmlNewText(BAD_CAST"NODE CONTENT");
xmlAddChild(root_node,node);
xmlAddChild(node,content);
xmlNewProp(node,BAD_CAST"attribute",BAD_CAST "yes"); //创建一个儿子和孙子节点
node = xmlNewNode(NULL, BAD_CAST "son");
xmlAddChild(root_node,node);
xmlNodePtr grandson = xmlNewNode(NULL, BAD_CAST "grandson");
xmlAddChild(node,grandson);
xmlAddChild(grandson, xmlNewText(BAD_CAST "This is a grandson node")); //存储xml文档
int nRel = xmlSaveFile("CreatedXml.xml",doc);
if (nRel != -)
{
cout<<"一个xml文档被创建,写入"<<nRel<<"个字节"<<endl;
} //释放文档内节点动态申请的内存
xmlFreeDoc(doc);
return ;
}
1.2、CreatedXml.xml
<?xml version="1.0"?>
<root>
<newNode1>newNode1 content</newNode1>
<newNode2>newNode2 content</newNode2>
<newNode3>newNode3 content</newNode3>
<node2 attribute="yes">NODE CONTENT</node2>
<son>
<grandson>This is a grandson node</grandson>
</son>
</root>
2、解析xml文档
2.1、ParseXmlFile.cpp
/********************************************************************
created: 2007/11/15
created: 15:11:2007 11:47
filename: ParseXmlFile.cpp
author: Wang xuebin
depend: libxml2.lib
build: nmake TARGET_NAME=ParseXmlFile purpose: 解析xml文件
*********************************************************************/ #include <libxml/parser.h>
#include <iostream.h> int main(int argc, char* argv[])
{
xmlDocPtr doc; //定义解析文档指针
xmlNodePtr curNode; //定义结点指针(你需要它为了在各个结点间移动)
xmlChar *szKey; //临时字符串变量 char *szDocName;
if (argc <= )
{
printf("Usage: %s docname\n", argv[]);
return();
}
szDocName = argv[]; doc = xmlReadFile(szDocName,"GB2312",XML_PARSE_RECOVER); //解析文件 //检查解析文档是否成功,如果不成功,libxml将指一个注册的错误并停止。
//一个常见错误是不适当的编码。XML标准文档除了用UTF-8或UTF-16外还可用其它编码保存。
//如果文档是这样,libxml将自动地为你转换到UTF-8。更多关于XML编码信息包含在XML标准中.
if (NULL == doc)
{
fprintf(stderr,"Document not parsed successfully. \n");
return -;
} curNode = xmlDocGetRootElement(doc); //确定文档根元素 /*检查确认当前文档中包含内容*/
if (NULL == curNode)
{
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return -;
} /*在这个例子中,我们需要确认文档是正确的类型。“root”是在这个示例中使用文档的根类型。*/
if (xmlStrcmp(curNode->name, BAD_CAST "root"))
{
fprintf(stderr,"document of the wrong type, root node != root");
xmlFreeDoc(doc);
return -;
} curNode = curNode->xmlChildrenNode;
xmlNodePtr propNodePtr = curNode;
while(curNode != NULL)
{
//取出节点中的内容
if ((!xmlStrcmp(curNode->name, (const xmlChar *)"newNode1")))
{
szKey = xmlNodeGetContent(curNode);
printf("newNode1: %s\n", szKey);
xmlFree(szKey);
} //查找带有属性attribute的节点
if (xmlHasProp(curNode,BAD_CAST "attribute"))
{
propNodePtr = curNode;
}
curNode = curNode->next;
} //查找属性
xmlAttrPtr attrPtr = propNodePtr->properties;
while (attrPtr != NULL)
{
if (!xmlStrcmp(attrPtr->name, BAD_CAST "attribute"))
{
xmlChar* szAttr = xmlGetProp(propNodePtr,BAD_CAST "attribute");
cout<<"get attribute = "<<szAttr<<endl;
xmlFree(szAttr);
}
attrPtr = attrPtr->next;
} xmlFreeDoc(doc);
return ;
}
2.2、ParseXmlFile.exe CreatedXml.xml
3、修改xml文档
3.1、ChangeXmlFile.cpp
/********************************************************************
created: 2007/11/15
created: 15:11:2007 15:20
filename: ChangeXmlFile.cpp
author: Wang xuebin
depend: libxml2.lib
build: nmake TARGET_NAME=ChangeXmlFile purpose: 修改XML元素及属性
*********************************************************************/ #include <libxml/parser.h>
#include <iostream.h> int main(int argc, char* argv[])
{
xmlDocPtr doc; //定义解析文档指针
xmlNodePtr curNode; //定义结点指针(你需要它为了在各个结点间移动) char *szDocName;
if (argc <= )
{
printf("Usage: %s docname\n", argv[]);
return();
}
szDocName = argv[]; doc = xmlReadFile(szDocName,"GB2312",XML_PARSE_RECOVER); //解析文件 if (NULL == doc)
{
fprintf(stderr,"Document not parsed successfully. \n");
return -;
} curNode = xmlDocGetRootElement(doc);
/*检查确认当前文档中包含内容*/
if (NULL == curNode)
{
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return -;
} curNode = curNode->children;
while (NULL != curNode)
{
//删除newNode1
if (!xmlStrcmp(curNode->name, BAD_CAST "newNode1"))
{
xmlNodePtr tempNode;
tempNode = curNode->next;
xmlUnlinkNode(curNode);
xmlFreeNode(curNode);
curNode = tempNode;
continue;
} //修改node2的属性值
if (!xmlStrcmp(curNode->name, BAD_CAST "node2"))
{
xmlSetProp(curNode,BAD_CAST "attribute", BAD_CAST "no");
}
//修改newNode2的内容
if (!xmlStrcmp(curNode->name, BAD_CAST "newNode2"))
{
xmlNodeSetContent(curNode, BAD_CAST "content changed");
} //增加一个属性
if (!xmlStrcmp(curNode->name, BAD_CAST "newNode3"))
{
xmlNewProp(curNode, BAD_CAST "newAttr", BAD_CAST "YES");
} //增加一个子节点
if (!xmlStrcmp(curNode->name, BAD_CAST "son"))
{
xmlNewTextChild(curNode, NULL, BAD_CAST "newGrandSon", BAD_CAST "new content");
} curNode = curNode->next;
} //存储xml文档
int nRel = xmlSaveFile("ChangedXml.xml",doc);
if (nRel != -)
{
cout<<"一个xml文档被创建,写入"<<nRel<<"个字节"<<endl;
}
xmlFreeDoc(doc);
return ;
}
3.2、ChangedXml.xml (ChangeXmlFile.exe CreatedXml.xml)
<?xml version="1.0"?>
<root> <newNode2>content changed</newNode2>
<newNode3 newAttr="YES">newNode3 content</newNode3>
<node2 attribute="no">NODE CONTENT</node2>
<son>
<grandson>This is a grandson node</grandson>
<newGrandSon>new content</newGrandSon></son>
</root>
4、使用XPATH查找xml文档
4.1、XPathForXmlFile.cpp
/********************************************************************
created: 2007/11/15
created: 15:11:2007 16:01
filename: XpathForXmlFile.cpp
author: Wang xuebin
depend: libxml2.lib
build: nmake TARGET_NAME=XPathForXmlFile purpose: 使用XPATH查找xml文档中的节点
*********************************************************************/ #include <libxml/parser.h>
#include <libxml/xpath.h>
#include <iostream.h> xmlXPathObjectPtr get_nodeset(xmlDocPtr doc, const xmlChar *szXpath)
{
xmlXPathContextPtr context; //XPATH上下文指针
xmlXPathObjectPtr result; //XPATH对象指针,用来存储查询结果 context = xmlXPathNewContext(doc); //创建一个XPath上下文指针
if (context == NULL)
{
printf("context is NULL\n");
return NULL;
} result = xmlXPathEvalExpression(szXpath, context); //查询XPath表达式,得到一个查询结果
xmlXPathFreeContext(context); //释放上下文指针
if (result == NULL)
{
printf("xmlXPathEvalExpression return NULL\n");
return NULL;
} if (xmlXPathNodeSetIsEmpty(result->nodesetval)) //检查查询结果是否为空
{
xmlXPathFreeObject(result);
printf("nodeset is empty\n");
return NULL;
} return result;
} int main(int argc, char* argv[])
{
xmlDocPtr doc = NULL; //定义解析文档指针
xmlNodePtr curNode = NULL; //定义结点指针(你需要它为了在各个结点间移动) char *szDocName = NULL;
if (argc <= )
{
printf("Usage: %s docname\n", argv[]);
return();
}
szDocName = argv[]; doc = xmlReadFile(szDocName,"GB2312",XML_PARSE_RECOVER); //解析文件 if (NULL == doc)
{
fprintf(stderr,"Document not parsed successfully. \n");
return -;
} xmlChar *szXpath =BAD_CAST ("/root/node2[@attribute='yes']");
xmlXPathObjectPtr app_result = get_nodeset(doc,szXpath); //查询并得到结果 if (NULL == app_result)
{
printf("app_result is NULL\n");
return -;
}
xmlChar *szValue = NULL;
if(app_result)
{
xmlNodeSetPtr nodeset = app_result->nodesetval;
for (int i = ; i < nodeset->nodeNr; i++)
{
curNode = nodeset->nodeTab[i];
if(curNode != NULL)
{
szValue = xmlGetProp(curNode,BAD_CAST "attribute");
if (szValue != NULL)
{
printf("attribute = %s\n", szValue);
xmlFree(szValue);
} szValue = xmlNodeGetContent(curNode);
if (szValue != NULL)
{
printf("content = %s\n", szValue);
xmlFree(szValue);
}
}
}
xmlXPathFreeObject (app_result);
}
xmlFreeDoc(doc);
return ;
}
4.2、XpathForXmlFile.exe CreatedXml.xml
5、用ICONV解决XML中的中文问题
5.1、wxb_codeConv.c
/********************************************************************
created: 2007/11/15
created: 15:11:2007 10:30
filename: wxb_codeConv.c
author: Wang xuebin
depend: iconv.lib
build: 不需要build,被包含到其它源代码中 purpose: 提供从UTF-8到GB2312的内码转换,以及反向的转换
*********************************************************************/ #include "iconv.h"
#include <string.h> //代码转换:从一种编码转为另一种编码
int code_convert(char* from_charset, char* to_charset, char* inbuf,
int inlen, char* outbuf, int outlen)
{
iconv_t cd;
char** pin = &inbuf;
char** pout = &outbuf; cd = iconv_open(to_charset,from_charset);
if(cd == )
return -;
memset(outbuf,,outlen);
if(iconv(cd,(const char**)pin,(unsigned int *)&inlen,pout,(unsigned int*)&outlen)
== -)
return -;
iconv_close(cd);
return ;
} //UNICODE码转为GB2312码
//成功则返回一个动态分配的char*变量,需要在使用完毕后手动free,失败返回NULL
char* u2g(char *inbuf)
{
int nOutLen = * strlen(inbuf) - ;
char* szOut = (char*)malloc(nOutLen); if (- == code_convert("utf-8","gb2312",inbuf,strlen(inbuf),szOut,nOutLen))
{
free(szOut);
szOut = NULL;
}
return szOut;
} //GB2312码转为UNICODE码
//成功则返回一个动态分配的char*变量,需要在使用完毕后手动free,失败返回NULL
char* g2u(char *inbuf)
{
int nOutLen = * strlen(inbuf) - ;
char* szOut = (char*)malloc(nOutLen); if (- == code_convert("gb2312","utf-8",inbuf,strlen(inbuf),szOut,nOutLen))
{
free(szOut);
szOut = NULL;
}
return szOut;
}
5.2、CreateXmlFile_cn.cpp
/********************************************************************
created: 2007/11/17
created: 9:11:2007 15:34
filename: CreateXmlFile.cpp
author: Wang xuebin
depend: libxml2.lib iconv.lib
build: nmake TARGET_NAME=CreateXmlFile_cn purpose: 创建一个xml文件,其中包含中文
*********************************************************************/ #include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <iostream.h>
#include "wxb_codeConv.c" //自己写的编码转换函数 int main(int argc, char **argv)
{
//定义文档和节点指针
xmlDocPtr doc = xmlNewDoc(BAD_CAST"1.0");
xmlNodePtr root_node = xmlNewNode(NULL,BAD_CAST"root"); //设置根节点
xmlDocSetRootElement(doc,root_node); //一个中文字符串转换为UTF-8字符串,然后写入
char* szOut = g2u("节点1的内容"); //在根节点中直接创建节点
xmlNewTextChild(root_node, NULL, BAD_CAST "newNode1", BAD_CAST "newNode1 content");
xmlNewTextChild(root_node, NULL, BAD_CAST "newNode2", BAD_CAST "newNode2 content");
xmlNewTextChild(root_node, NULL, BAD_CAST "newNode3", BAD_CAST "newNode3 content"); xmlNewChild(root_node, NULL, BAD_CAST "node1",BAD_CAST szOut);
free(szOut); //创建一个节点,设置其内容和属性,然后加入根结点
xmlNodePtr node = xmlNewNode(NULL,BAD_CAST"node2");
xmlNodePtr content = xmlNewText(BAD_CAST"NODE CONTENT");
xmlAddChild(root_node,node);
xmlAddChild(node,content);
szOut = g2u("属性值");
xmlNewProp(node,BAD_CAST"attribute",BAD_CAST szOut);
free(szOut); //创建一个中文节点
szOut = g2u("中文节点");
xmlNewChild(root_node, NULL, BAD_CAST szOut,BAD_CAST "content of chinese node");
free(szOut); //存储xml文档
int nRel = xmlSaveFormatFileEnc("CreatedXml_cn.xml",doc,"GB2312",);
if (nRel != -)
{
cout<<"一个xml文档被创建,写入"<<nRel<<"个字节"<<endl;
} xmlFreeDoc(doc); return ;
}
5.3、CreatedXml_cn.xml
<?xml version="1.0" encoding="GB2312"?>
<root>
<newNode1>newNode1 content</newNode1>
<newNode2>newNode2 content</newNode2>
<newNode3>newNode3 content</newNode3>
<node1>节点1的内容</node1>
<node2 attribute="属性值">NODE CONTENT</node2>
<中文节点>content of chinese node</中文节点>
</root>
6、用XML来做点什么
6.1、1.xml
<?xml version="1.0" encoding="GB2312"?>
<root>
<My_Program_Code content="1"></My_Program_Code>
</root>
7、
8、
XML_CPP_资料_libXml2_01_Code的更多相关文章
- XML_CPP_资料_libXml2_01
ZC: 看了一些 C/C++的XML文章,也看了一些 Qt的 QXmlQuery/QXmlSimpleReader/QXmlStreamReader/QXmlStreamWriter 的文章.总体感觉 ...
- XML_CPP_资料_libXml2_01_Code_ZC(?.pro)
ZC:最下面有 ?.pro文件的设置写法 ZC: Win7x64,qt-opensource-windows-x86-msvc2010_opengl-5.3.2.exe,cn_visual_studi ...
- XML_CPP_资料_libxml2库函数详解
http://blog.csdn.net/hanchaoman/article/details/42557195 许多事物符合80/20法则,libxml中也是20%的函数提供了80%的功能.下面的列 ...
- XML_CPP_资料
1.TinyXML解析xml文档 - zhoubl668的专栏:远帆,梦之帆! - 博客频道 - CSDN.NET.html http://blog.csdn.net/zhoubl668/articl ...
- XML_CPP_libXml2_VC6_Code_ZC
ZC:iconv.dll.libxml2.dll.zlib1.dll 放到 exe所在目录下 1.代码来源于 帖子:XML_CPP_资料_libXml2_01_Code_ZC(?.pro) 2.代码: ...
- Vim新手入门资料和一些Vim实用小技巧
一些网络上质量较高的Vim资料 从我07年接触Vim以来,已经过去了8个年头,期间看过很多的Vim文章,我自己觉得非常不错,而且创作时间也比较近的文章有如下这些. Vim入门 目前为阿里巴巴高级技术专 ...
- Git入门资料汇总
Git是一个非常好用的版本控制工具,同时,它也是一个相对比较复杂的工具,想要掌握它还是需要花一番功夫的.网络上关于Git的入门资料已经很多了,我就不再重复了,直接把我学习的文章放在这里. Git详解 ...
- MVC5 网站开发之七 用户功能 3用户资料的修改和删除
这次主要实现管理后台界面用户资料的修改和删除,修改用户资料和角色是经常用到的功能,但删除用户的情况比较少,为了功能的完整性还是坐上了.主要用到两个action "Modify"和& ...
- webapi的学习资料
猿教程_-webapi教程-WebAPI教程 猿教程_-webapi教程-Web API概述 猿教程_-webapi教程-新建Web Api项目 猿教程_-webapi教程-测试Web API 猿教程 ...
随机推荐
- unity3d-射线(Ray)
射线Ray 射线是一个点向另外一个点发生的一条线,一旦与其他模型发生碰撞,他将停止发射.注意这条件是逻辑上的,界面上看不到. 一般使用射线判断是否发射至某个游戏对象上或者获得鼠标点击的游戏对象等. 用 ...
- Spring MVC 和 Struts2 的比较
SpringMVC与Struts2的比较 1:框架核心机制:SpringMVC(DispatcherServlet)采用Servlet实现,Struts2采用Filter(StrutsPrepareA ...
- mysql插入和更新时自动更新为当前时间
创建表的时候添加 CREATE TABLE `tmp` ( `id` varchar(32) NOT NULL, `update_time ` timestamp NOT NULL DEFAUL ...
- mysql性能优化1
当我们去设计数据库表结构,对操作数据库时(尤其是查表时的SQL语句),我们都需要注意数据操作的性能.这里,我们不会讲过多的SQL语句的优化,而只是针对MySQL这一Web应用最多的数据库.希望下面的这 ...
- Nuget的学习总结
Nuget的学习总结 今天研究了一下nuget,发现nuget实在是太有用了,便写下了这篇博客,希望记录一下自己的学习历程,也希望技术圈的朋友看到之后,如果里面哪里写的不够好,可以给我些宝贵的意见,以 ...
- Linux系统下C语言程序的构建过程
本文转载自:http://www.ruanyifeng.com/blog/2014/11/compiler.html 源码要运行,必须先转成二进制的机器码.这是编译器的任务. 比如,下面这段源码(假定 ...
- php new stdClass array 实例代码
php new stdClass array 实例代码 $searchResults = array ();// //$obj = array ("rs"=>array(), ...
- 03: MySQL基本操作
MySQL其他篇 目录: 参考网站 1.1 MySQL 三种数据类型(数值,字符串,日期) 1.2 MySQL常用增删改查命令 1.3 删除,添加或修改表字段 1.4 MySQL外键关联(一对多) 1 ...
- keepalived+nginx实现HA高可用的web负载均衡
Keepalived 是一种高性能的服务器高可用或热备解决方案, Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web 前端服务的高可用.Keepalived ...
- 20145304 网络对抗技术 逆向与Bof基础
20145304 网络对抗技术 逆向与Bof基础 实践目标 学习以下两种方法,运行正常情况下不会被运行的代码: 手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数. 利用foo函数的 ...