linux下xml和json的使用案例

xml
使用xml需要加入动态库

<?xml version="1.0" encoding="utf-8"?>
<China>
	<City>
		<Name isbig="true">北京</Name>
		<Area>1.641万平方千米</Area>
		<Population>2200万</Population>
	</City>
	<City>
		<Name isbig="false">石家庄</Name>
		<Area>15848平方千米</Area>
		<Population>107万</Population>
	</City>
</China>

创建xml文件

#include <stdio.h>
#include <mxml.h>

int main(int argc, const char* argv[])
{
    // 创建xml文件头节点
    mxml_node_t *xml = mxmlNewXML("1.0");
    // 创建xml根节点 - china
    mxml_node_t* china = mxmlNewElement(xml, "China");
    // 创建城市节点
    mxml_node_t* city = mxmlNewElement(china, "City");
    // 添加子节点
    // name
    mxml_node_t* name = mxmlNewElement(city, "Name");
    // 设置标签值
    mxmlNewText(name, 0, "北京");
    mxmlElementSetAttr(name, "isbig", "true");
    // 面积
    mxml_node_t* area = mxmlNewElement(city, "Area");
    mxmlNewText(area, 0, "1.641万平方千米");
    // 人口
    mxml_node_t* popu = mxmlNewElement(city, "Population");
    mxmlNewText(popu, 0, "2200万");
    // 第二个城市节点
    city = mxmlNewElement(china, "City");
    // name
    name = mxmlNewElement(city, "Name");
    mxmlNewText(name, 0, "石家庄");
    mxmlElementSetAttr(name, "isbig", "false");
    area = mxmlNewElement(city, "Area");
    mxmlNewText(area, 0, "15848平方千米");
    popu = mxmlNewElement(city, "Population");
    mxmlNewText(popu, 0, "107万");
    // 将xml内容保存到磁盘
    FILE* fp = fopen("china.xml", "w");
    mxmlSaveFile(xml, fp, MXML_NO_CALLBACK);
    fclose(fp);
    mxmlDelete(xml);
    return 0;
}

读取xml文件

#include <stdio.h>
#include <mxml.h>

int main(int argc, const char* argv[])
{
    // 从磁盘加载xml文件
    FILE* fp = fopen("china.xml", "r");
    if(fp == NULL)
    {
        printf("fopen error\n");
        return 0;
    }
    // root 节点指向xml文件头
    mxml_node_t* root = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
    // 遍历 - 取出各个节点的值
    // 找到第一个城市节点
    mxml_node_t* city = mxmlFindElement(root, root, "City", NULL, NULL, MXML_DESCEND);
    if(city == NULL)
    {
        printf("xml node not found\n");
        return 0;
    }
    while( city  )
    {
        printf("==================\n");
        // 向下走一个节点
        mxml_node_t* node = mxmlWalkNext(city, root, MXML_DESCEND_FIRST);
        printf("city:   \n");
        printf("    name = %s\n", mxmlGetText(node, NULL));
        // 
        node = mxmlWalkNext(node, root, MXML_NO_DESCEND);
        printf("    area = %s\n", mxmlGetText(node, NULL));
        //
        node = mxmlWalkNext(node, root, MXML_NO_DESCEND);
        printf("    population = %s\n", mxmlGetText(node, NULL));
        // 搜索下一个城市节点
        city = mxmlFindElement(city, root, "City", NULL, NULL, MXML_DESCEND);
    }
    fclose(fp);
    mxmlDelete(root);
    return 0;
}

json
使用json需要添加json的 .c 和.h文件

{
	"奔驰":	{
		"factory":	"一汽大众",
		"last":	31,
		"price":	83,
		"sell":	49,
		"sum":	80,
		"other":	[124, "hello, world", false]
	}
}

创建json文件

#include <stdio.h>
#include <string.h>
#include "cJSON.h"

int main(int argc, const char* argv[])
{
    // 创建json对象
    cJSON* obj = cJSON_CreateObject(); 
    // 创建子对象 - 品牌
    cJSON* brand = cJSON_CreateObject();
    // 添加键值对
    cJSON_AddItemToObject(brand, "factory", cJSON_CreateString("一汽大众"));
    cJSON_AddItemToObject(brand, "last", cJSON_CreateNumber(31));
    cJSON_AddItemToObject(brand, "price", cJSON_CreateNumber(83));
    cJSON_AddItemToObject(brand, "sell", cJSON_CreateNumber(49));
    cJSON_AddItemToObject(brand, "sum", cJSON_CreateNumber(80));
    // 创建json数组
    cJSON* array = cJSON_CreateArray();
    cJSON_AddItemToArray(array, cJSON_CreateNumber(124));
    cJSON_AddItemToArray(array, cJSON_CreateString("hello, world"));
    cJSON_AddItemToArray(array, cJSON_CreateBool(0));
    cJSON_AddItemToObject(brand, "other", array);
    cJSON_AddItemToObject(obj, "奔驰", brand);
    // 格式化json对象
    char* text = cJSON_Print(obj);
    FILE* fp = fopen("car.json", "w");
    fwrite(text, 1, strlen(text), fp);
    fclose(fp);
    return 0;
}

读取json文件

#include <stdio.h>
#include <string.h>
#include "cJSON.h"

int main(int argc, const char* argv[])
{
    if(argc < 2)
    {
        printf("./a.out jsonfile\n");
        return 0;
    }

    // 加载json文件 
    FILE* fp = fopen(argv[1], "r");
    char buf[1024] = {0};
    fread(buf, 1, sizeof(buf), fp);
    cJSON* root = cJSON_Parse(buf);

    cJSON* subobj = cJSON_GetObjectItem(root, "奔驰");
    // 判断对象是否存在
    if( subobj )
    {
        // 获取子对象
        cJSON* factory = cJSON_GetObjectItem(subobj, "factory");
        cJSON* last = cJSON_GetObjectItem(subobj, "last");
        cJSON* price = cJSON_GetObjectItem(subobj, "price");
        cJSON* sell = cJSON_GetObjectItem(subobj, "sell");
        cJSON* sum = cJSON_GetObjectItem(subobj, "sum");
        cJSON* other = cJSON_GetObjectItem(subobj, "other");
        // 打印value值
        printf("奔驰:\n");
        printf("    factory: %s\n", cJSON_Print(factory));
        printf("    last: %s\n", cJSON_Print(last));
        printf("    price: %s\n", cJSON_Print(price));
        printf("    sell: %s\n", cJSON_Print(sell));
        printf("    sum: %s\n", cJSON_Print(sum));
        // 打印数组内容
        printf("    other:\n");
        if(other->type == cJSON_Array)
        {
            for(int i=0; i<cJSON_GetArraySize(other); ++i)
            {
                cJSON* node = cJSON_GetArrayItem(other, i);
                // 判断数据类型
                if(node->type == cJSON_String)
                {
                    printf("        %s  \n", node->valuestring);
                }
                if(node->type == cJSON_Number)
                {
                    printf("        %d\n", node->valueint);
                }
                if(node->type == cJSON_True)
                {
                    printf("        %d\n", node->valueint);
                }
                if(node->type == cJSON_False)
                {
                    printf("        %d\n", node->valueint);
                }
            }
        }
    }
    cJSON_Delete(root);
    fclose(fp);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值