xml文件的解析(libxml2)
2015-06-09 09:37
495 查看
#include <stdio.h>
#include <libxml/parser.h>
int main(int argc, char* argv[])
{
xmlDocPtr doc; //定义解析文档指针
xmlNodePtr curNode; //定义结点指针(你需要它为了在各个结点间移动)
xmlChar *szKey; //临时字符串变量
char *szDocName;
if (argc <= 1) {
printf("Usage: %s docname\n", argv[0]);
return -1;
}
szDocName = argv[1];
doc = xmlReadFile(szDocName, "GB2312", XML_PARSE_RECOVER); //以GB2312的编码方式读入解析文件
//检查解析文档是否成功,如果不成功,libxml将指一个注册的错误并停止。
//一个常见错误是不适当的编码。XML标准文档除了用UTF-8或UTF-16外还可用其它编码保存。
//如果文档是这样,libxml将自动地为你转换到UTF-8。更多关于XML编码信息包含在XML标准中.
if (NULL == doc) {
fprintf(stderr,"Document is not parsed successfully. \n");
return -1;
}
curNode = xmlDocGetRootElement(doc); //确定文档根元素
/*检查确认当前文档中包含内容*/
if (NULL == curNode) {
fprintf(stderr,"the document is empty.\n");
xmlFreeDoc(doc);
return -1;
}
/*在这个例子中,我们需要确认文档是正确的类型。“root”是在这个示例中使用文档的根类型。*/
if (xmlStrcmp(curNode->name, BAD_CAST "root")) {
fprintf(stderr,"document of the wrong type, root_node is not root");
xmlFreeDoc(doc);
return -1;
}
curNode = curNode->xmlChildrenNode;
xmlNodePtr propNodePtr = curNode;
while(NULL != curNode) {
//取出节点中的内容
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");
printf("get attribute = %s\n", szAttr);
xmlFree(szAttr);
}
attrPtr = attrPtr->next;
}
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}
#include <libxml/parser.h>
int main(int argc, char* argv[])
{
xmlDocPtr doc; //定义解析文档指针
xmlNodePtr curNode; //定义结点指针(你需要它为了在各个结点间移动)
xmlChar *szKey; //临时字符串变量
char *szDocName;
if (argc <= 1) {
printf("Usage: %s docname\n", argv[0]);
return -1;
}
szDocName = argv[1];
doc = xmlReadFile(szDocName, "GB2312", XML_PARSE_RECOVER); //以GB2312的编码方式读入解析文件
//检查解析文档是否成功,如果不成功,libxml将指一个注册的错误并停止。
//一个常见错误是不适当的编码。XML标准文档除了用UTF-8或UTF-16外还可用其它编码保存。
//如果文档是这样,libxml将自动地为你转换到UTF-8。更多关于XML编码信息包含在XML标准中.
if (NULL == doc) {
fprintf(stderr,"Document is not parsed successfully. \n");
return -1;
}
curNode = xmlDocGetRootElement(doc); //确定文档根元素
/*检查确认当前文档中包含内容*/
if (NULL == curNode) {
fprintf(stderr,"the document is empty.\n");
xmlFreeDoc(doc);
return -1;
}
/*在这个例子中,我们需要确认文档是正确的类型。“root”是在这个示例中使用文档的根类型。*/
if (xmlStrcmp(curNode->name, BAD_CAST "root")) {
fprintf(stderr,"document of the wrong type, root_node is not root");
xmlFreeDoc(doc);
return -1;
}
curNode = curNode->xmlChildrenNode;
xmlNodePtr propNodePtr = curNode;
while(NULL != curNode) {
//取出节点中的内容
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");
printf("get attribute = %s\n", szAttr);
xmlFree(szAttr);
}
attrPtr = attrPtr->next;
}
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}
相关文章推荐
- 2.MFC简单的层次结构分析
- 如何在HiWork中进行文件预览
- RS查询报错之递归公用表表达式不包含顶级 UNION ALL运算符
- 怎么样进行轻松高效的代码复读
- Python 学习笔记2
- 深入理解Java:注解(Annotation)--注解处理器
- UltraEdit (Ctrl + F) 查找、(Ctrl + R)替换功能失效
- jQuery Validation Engine 表单验证
- bind和live的区别
- Linux的IPC命令
- centos下 ifconfig的安装
- VISUAL SVN安装 及客户端使用
- hdu 5239 Doom(线段树+数论)
- 沉浸式状态栏调用方法
- C++中的static关键字
- 正则表达式30分钟入门教程
- ios的notification机制是同步的还是异步的
- VISUAL SVN安装 及客户端使用
- VISUAL SVN安装 及客户端使用
- maven pom查询