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 猿教程 ...
随机推荐
- win10 + cuda(v9.0) 安装TensorFlow-gpu版
之前在实习公司的电脑上装过TensorFlow-gpu,那时候很快就装好了.但在自己的笔记本上装时,却搞了很久... 一部分原因是因为用校园网下载cuda toolkit 和cudnn ,总是在最后时 ...
- [redis] 介绍安装
redis相关网站 官方网站:http://redis.io/ redis简介 官方介绍:http://redis.io/topics/introduction 百度百科:http://baike.b ...
- inline详解
1. 引入inline关键字的原因 在c/c++中,为了解决一些频繁调用的小函数大量消耗栈空间(栈内存)的问题,特别的引入了inline修饰符,表示为内联函数. 栈空间就是指放置程序的局部数据(也就是 ...
- linux chkconfig 管理服务开机自启动
chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig [--ad ...
- 如何在Qt Creator中创建pri文件,以及pri文件的说明
初学Qt的人可还不会接触到这个问题,但是一旦你开始编写某个较大项目的时候,这个问题就不可避免需要解决. 对于大神们来讲可能这是个很简单的问题,但是对于新手来说,想要搞清楚需要下很大功夫. 怎么创建pr ...
- P4391 [BOI2009]Radio Transmission 无线传输
P4391 [BOI2009]Radio Transmission 无线传输 kmp 题目让我们求一个串的最小循环子串 我们回想一下kmp中的失配函数 用 f 数组保存当前字符匹配失败后,需要跳到的前 ...
- 20145227鄢曼君《网络对抗》MSF基础应用
20145227鄢曼君<网络对抗>MSF基础应用 主动攻击:ms08_067漏洞攻击实践 两台虚拟机,其中一台为kali,一台为windows xp sp3(英文版).在VMware中设置 ...
- 20145318《网络对抗》注入shellcode及Return-to-libc
20145318<网络对抗>注入shellcode及Return-to-libc 注入shellcode 知识点 注入shellcodeShellcode实际是一段代码(也可以是填充数据) ...
- 在CentOS Linux系统上,添加新的端口,启用ssh服务
SSH作为Linux远程连接重要的方式,如何配置安装linux系统的SSH服务,如何开启SSH? SSH是什么? SSH 为 Secure Shell 由 IETF 的网络工作小组(Network W ...
- luogu3261 懒惰左偏树 [JLOI2015]城池攻占
目录 题目 思路 错误&&反思 代码 题目 luogu 原来左偏树真的能懒惰下放 那这篇博客应该要咕咕了 一开始我按照那篇博客想了一下,感觉emm,还是瞄了一眼看到了pushdown ...