一文看懂XML的创建/解析

参考链接

[1]xml是树形结构

在这里插入图片描述

[2]xml的基本语法

  1. xml文件头 <?xml version=“1.0” encodeing=“utf-8”> //version不可省略 encodeing可以省略 ===> 使用mxmlNewXML创建的就是这行内容
  2. 标签必须有起始<class>和关闭</class>,即必须成对出现;不能向HTML那样只出现起始<class>而不出现</class>
  3. 对大小写敏感
  4. xml属性值必须加引号,如<note time = “10:20”>;并且一个node可以包含多个属性。
  5. 注释<!--注释内容-->

工具:百度搜索“xml在线格式化工具”,检查写好的xml是否格式正确。

[3]mxml (mini-xml)安装

  1. 下载https://github.com/michaelrsweet/mxml/releases
  2. 查看README,按照README的过程安装(如果实在还是不会,请自行百度)

[4]使用库[创建]一个xml文件

(1)总步骤

  1. 包含头文件: #include <mxml.h>
  2. 打开一个本地文件fd:FILE* fd = fopen(“new.xml”,“w+”);
  3. 创建一个新的xml文件:mxml_node_t* root = mxmlNewXML(“1.0”);
  4. 创建一个新节点:mxml_node_t* Class = mxmlNewElement(root,“Class”);
  5. 给节点设置添加属性:mxmlElementSetAttr(Class,“language”,“C++”);
  6. 给节点添加文本:mxmlNewText(Class,0,“ZhangSan”);
  7. 保存fd到本地:mxmlSaveFile(root,fd,NULL);
  8. 关闭fd:fclose(fd);

(2)函数API

头文件#include <mxml.h> --> 依赖config.h
mxml_node_t *mxmlNewXML(const char *version);新创建的xml文件节点<?xml version=“1.0” encodeing=“utf-8”>
mxml_node_t *mxmlNewElement(mxml_node_t *parent,const char* name);创建一个新节点,它的父节点是parent。参数:parent节点指针;name新节点名称
void mxmlElementSetAttr(mxml_node_t *node,const char* name,const char* value);给node节点设置属性名和属性值
mxml_node_t *mxmlNewText(mxml_node_t *node,int whitespace,const char* string)给node节点添加文本string
int mxmlSaveFile(mxml_node_t *node,FILE* fp,mxml_save_cb_t cb);保存节点到本地的xml文件。参数:node-树根;fp-fopen打开的文件;cb-一般传NULL

示例代码1:<node tip=“爽”></node>

mxml_node_t* node = mxmlNewElement(root, "node"); //创建node
mxmlElementSetAttr(node, "tip", "爽"); //给node的添加属性tip="爽"

示例代码2:<node>I am a student</node>

mxml_node_t* node = mxmlNewElement(root, "node"); //创建node
mxmlNewText(node, 0, "I am a student"); //给node添加文本I am a student

[5]使用库[解析]一个xml文件

(1)总步骤

使用代码案例来描述总步骤
在这里插入图片描述

(2)函数API

mxml_node_t* mxml_LoadFile(mxml_node_t* top,FILE* fp,mxml_type_t (*cb)(mxml_node_t*));将fd加载到内存:mxml_LoadFile(NULL,fd,MXML_NO_CALLBACK);
void mxmlDelete(mxml_node_t* node);释放加载到内存的node
const char* mxmlElementGetAttr(mxml_node_t* node,const char* name);返回node节点的tip属性的属性值,<node tip=“爽”></node>,返回值 = “爽”
const char* mxmlGetText(mxml_node_t* node,int* whitespace);返回node节点的文本,<node>I am a student</node>,返回值 = I am a student
mxml_node_t* mxmlWalkNext(mxml_node_t* node,mxml_node_t* top,int descend);跳转到下一个节点

mxml_node_t* ret = mxmlFindElemen(mxml_node_t*node,mxml_node_t* top,const char* name,const char* attr,const char* value, int descend);
说明:最难的就是mxmlFindElemen函数,函数的node参数一定要搞懂,表示从node节点的下一个节点开始查找!
返回值:查找到,返回查找位置;没查找到,返回NULL
参数

  • node:从node开始向下面各行查找,查找的结果不包括node。
  • top:顶层节点,一般都设置为mxml_LoadFile的返回值
  • name:node节点的名字
  • attr、value:node节点的属性名、属性名对应的属性值
  • descend:一般只使用MXML_DESCEND
descend
MXML_NO_DESCEND不查看任何子节点在XML元素层次中,仅查看同层级别的节点或者父节点直到到达根节点或者top节点
MXML_DESCEND_FIRST向下搜索到第一个节点的第一个匹配子节点,就不再继续向下搜索(一般用于遍历一个父节点的直接子节点)
MXML_DESCEND基本都使用该选项,表示一直向下搜索

mxml_node_t* mxmlWalkNext(mxml_node_t* node,mxml_node_t* top,int descend);


[6]示例代码

解析QQ数据中的[原图地址]、[缩略图地址],并打印


在这里插入图片描述
xml文档

<?xml version="1.0" encoding="utf-8"?>

<!--pkgName对应GetResourceRespInfo的strPkgName字段-->
<!--version对应GetResourceRespInfo的uiNewVer字段-->
<PicArray version="1"  pkgName="QQCHATPic_hdpi" >
    <Pic>
        <!-- 原图地址--> 
        <resurl md5 = "qq2013_chatbg101_hdpi">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/720V2/chat_bg_101_720.jpg</resurl> 
        <!-- 缩略图地址-->  
        <thumbnail md5 = "qq2013_chatbg_thumb_101">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/thumb/chatbg_thumb_101..png</thumbnail>    
    </Pic>
    <Pic>
        <!-- 原图地址--> 
        <resurl md5 = "qq2013_chatbg102_hdpi">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/720V2/chat_bg_102_720.jpg</resurl> 
        <!-- 缩略图地址-->  
        <thumbnail md5 = "qq2013_chatbg_thumb_102">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/thumb/chatbg_thumb_102..png</thumbnail>    
    </Pic>
    <Pic>
        <!-- 原图地址--> 
        <resurl md5 = "qq2013_chatbg103_hdpi">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/720V2/chat_bg_103_720.jpg</resurl> 
        <!-- 缩略图地址-->  
        <thumbnail md5 = "qq2013_chatbg_thumb_103">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/thumb/chatbg_thumb_103..png</thumbnail>    
    </Pic>
    
    <Pic>
        <!-- 原图地址--> 
        <resurl md5 = "qq2013_chatbg104_hdpi">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/720V2/chat_bg_104_720.jpg</resurl> 
        <!-- 缩略图地址-->  
        <thumbnail md5 = "qq2013_chatbg_thumb_104">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/thumb/chatbg_thumb_104..png</thumbnail>    
    </Pic>
    
    <Pic>
        <!-- 原图地址--> 
        <resurl md5 = "qq2013_chatbg105_hdpi">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/720V2/chat_bg_105_720.jpg</resurl> 
        <!-- 缩略图地址-->  
        <thumbnail md5 = "qq2013_chatbg_thumb_105">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/thumb/chatbg_thumb_105..png</thumbnail>    
    </Pic>
    
    <Pic>
        <!-- 原图地址--> 
        <resurl md5 = "qq2013_chatbg106_hdpi">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/720V2/chat_bg_106_720.jpg</resurl> 
        <!-- 缩略图地址-->  
        <thumbnail md5 = "qq2013_chatbg_thumb_106">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/thumb/chatbg_thumb_106..png</thumbnail>    
    </Pic>
    
    <Pic>
        <!-- 原图地址--> 
        <resurl md5 = "qq2013_chatbg107_hdpi">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/720V2/chat_bg_107_720.jpg</resurl> 
        <!-- 缩略图地址-->  
        <thumbnail md5 = "qq2013_chatbg_thumb_107">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/thumb/chatbg_thumb_107..png</thumbnail>    
    </Pic>
    
    <Pic>
        <!-- 原图地址--> 
        <resurl md5 = "qq2013_chatbg108_hdpi">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/720V2/chat_bg_108_720.jpg</resurl> 
        <!-- 缩略图地址-->  
        <thumbnail md5 = "qq2013_chatbg_thumb_108">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/thumb/chatbg_thumb_108..png</thumbnail>    
    </Pic>
    <Pic>
        <!-- 原图地址--> 
        <resurl md5 = "qq2013_chatbg109_hdpi">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/720V2/chat_bg_109_720.jpg</resurl> 
        <!-- 缩略图地址-->  
        <thumbnail md5 = "qq2013_chatbg_thumb_109">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/thumb/chatbg_thumb_109..png</thumbnail>    
    </Pic>    
    <Pic>
        <!-- 原图地址--> 
        <resurl md5 = "qq2013_chatbg1101_hdpi">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/720V2/chat_bg_110_720.jpg</resurl> 
        <!-- 缩略图地址-->  
        <thumbnail md5 = "qq2013_chatbg_thumb_1101">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/thumb/chat_bg_thumb_110..png</thumbnail>    
    </Pic>    
    <Pic>
        <!-- 原图地址--> 
        <resurl md5 = "qq2013_chatbg1112_hdpi">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/720V2/chat_bg_111_720.jpg</resurl> 
        <!-- 缩略图地址-->  
        <thumbnail md5 = "qq2013_chatbg_thumb_1112">http://appimg1.3g.qq.com/msoft/mobileQQ_theme/new_background/thumb/chatbg_thumb_111..png</thumbnail>    
    </Pic>      
  </PicArray>
//#include <config.h>
#include <mxml.h>
#include <stdio.h>
int main()
{
  FILE* fd = fopen("new.xml","r+");
  mxml_node_t* root = mxmlLoadFile(NULL,fd,MXML_NO_CALLBACK);
  
  mxml_node_t* ret = mxmlFindElement(root,root,"Pic",NULL,NULL,MXML_DESCEND);

  while(ret)
  {
    mxml_node_t* resurl = mxmlFindElement(ret,root,"resurl","md5",NULL,MXML_DESCEND);
    printf("%s,%s\n",mxmlElementGetAttr(resurl,"md5"),mxmlGetText(resurl,0));
    mxml_node_t* thumbnail = mxmlFindElement(resurl,root,"thumbnail","md5",NULL,MXML_DESCEND);
    printf("%s,%s\n",mxmlElementGetAttr(thumbnail,"md5"),mxmlGetText(thumbnail,0));

    ret = mxmlFindElement(ret,root,"Pic",NULL,NULL,MXML_DESCEND);
    printf("\n");
  }

  mxmlDelete(root);
  fclose(fd);
  
  return 0;
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值