web服务器开发复习动态库找不到问题
mxml安装和配置
./configure
make
sudo make install
测试安装是否成功。
出现以上问题是因为只有声明没有实现,需要链接一些库。
输入命令:
gcc testmxml.c -lmxml -lpthread -o mytest
./mytest
出现错误:
./mytest: error while loading shared libraries: libmxml.so.1: cannot open shared object file: No such file or directory
解决办法:
这个问题是没有配置环境变量LD_LIBRARY_PATH=/usr/local/lib
使用export LD_LIBRARY_PATH=/usr/local/lib
./mytest后,Usage: testmxml filename.xml [string-output.xml]
需要./mytest test.xml即可。
mxml
包含头文件:mxml.h
编译的时候需要添加动态库:libmxml.so-lmxml
/usr/local/lib
若想编译的时候不加-lpthread,可以关闭。
./configure --enable-threads=no
使用mxml api生成xml文件
根标签的对应的节点的父亲节点是文件头节点。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <mxml.h>
int main(int argc, const char* argv[])
{
// 文件头
mxml_node_t *root = mxmlNewXML("1.0");
// 根标签 -- china
mxml_node_t* china = mxmlNewElement(root, "china");
// 子标签 -- city
mxml_node_t* city = mxmlNewElement(china, "city");
mxml_node_t* info = mxmlNewElement(city, "name");
// 标签赋值
mxmlNewText(info, 0, "北京");
// 设置属性
mxmlElementSetAttr(info, "isbig", "Yes");
// 面积
info = mxmlNewElement(city, "area");
mxmlNewText(info, 0, "16410 平方公里");
// 人口
info = mxmlNewElement(city, "population");
mxmlNewText(info, 0, "2171万人");
// gdp
info = mxmlNewElement(city, "gdp");
mxmlNewText(info, 0, "24541亿元");
// 东京
city = mxmlNewElement(china, "city");
info = mxmlNewElement(city, "name");
// 标签赋值
mxmlNewText(info, 0, "东京");
// 设置属性
mxmlElementSetAttr(info, "isbig", "No");
// 面积
info = mxmlNewElement(city, "area");
mxmlNewText(info, 0, "2188 平方公里");
// 人口
info = mxmlNewElement(city, "population");
mxmlNewText(info, 0, "3670万人");
// gdp
info = mxmlNewElement(city, "gdp");
mxmlNewText(info, 0, "31700亿元");
// 数据保存到磁盘文件
FILE* fp = fopen("china.xml", "w");
mxmlSaveFile(root, fp, MXML_NO_CALLBACK);
fclose(fp);
mxmlDelete(root);
return 0;
}
mxml获取文件数据api
#include <stdio.h>
#include "mxml.h"
int main(int argc, const char* argv[])
{
if(argc < 2)
{
printf("./a.out filename\n");
return 0;
}
// 加载xml文件
FILE* fp = fopen(argv[1], "r");
mxml_node_t* root = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
// 找到第一个factory节点
mxml_node_t* factory = mxmlFindElement(root, root, "factory", "name", NULL, MXML_DESCEND);
// 循环查找
while( factory )
{
// 打印几点的属性值
printf("factory attr: %s\n", mxmlElementGetAttr(factory, "name"));
// 向下移动一个节点
mxml_node_t* brand = mxmlWalkNext(factory, root, MXML_DESCEND);
while( brand )
{
// name
mxml_node_t* node = mxmlWalkNext(brand, root, MXML_DESCEND_FIRST);
printf(" name: %s\n", mxmlGetText(node, 0));
// color
node = mxmlWalkNext(node, root, MXML_NO_DESCEND);
printf(" color: %s\n", mxmlGetText(node, 0));
// price
node = mxmlWalkNext(node, root, MXML_NO_DESCEND);
printf(" price: %s\n", mxmlGetText(node, 0));
printf(" =========================================\n");
// 找到下一个品牌节点
brand = mxmlFindElement(brand, root, "brand", NULL, NULL, MXML_NO_DESCEND);
}
// 打印该厂家对应的车辆品牌和属性信息
// 查找下一个节点
factory = mxmlFindElement(factory, root, "factory", "name", NULL, MXML_NO_DESCEND);
}
mxmlDelete(root);
fclose(fp);
return 0;
}
json格式
json格式示例文件
[
"一汽大众",
"二汽神龙",
"上海大众",
123,
true
]
{
"KSF": "康师傅",
"Kevin": "Apple",
"Leilei": "loveHanMeiMei",
"Licy": "red",
"Lucy": "HelloWorld"
}
{
"奔驰": {
"factory": "一汽大众",
"last": 31,
"price": 83,
"sell": 49,
"sum": 80,
},
"奥迪A6": {
"factory": "一汽大众",
"last": 15,
"price": 36,
"sell": 35,
"sum": 50
},
"宝来": {
"factory": "一汽大众",
"last": 75,
"price": 41,
"sell": 5,
"sum": 80
},
"富康": {
"factory": "二汽神龙",
"last": 38,
"price": 28,
"sell": 22,
"sum": 60
},
"帕萨特": {
"factory": "上海大众",
"last": 42,
"price": 27,
"sell": 23,
"sum": 65
},
"捷达": {
"factory": "一汽大众",
"last": 69,
"price": 10,
"sell": 11,
"sum": 80
},
"标致307": {
"factory": "二汽神龙",
"last": 58,
"price": 27,
"sell": 12,
"sum": 70
},
"桑塔纳": {
"factory": "上海大众",
"last": 63,
"price": 25,
"sell": 12,
"sum": 75
},
"毕加索": {
"factory": "二汽神龙",
"last": 29,
"price": 39,
"sell": 21,
"sum": 50
}
}
cjson介绍
JSON(JavaScript Object Notation)是一种轻量级的文本数据交换格式,易于让人阅读。同时也易于机器解析和生成。尽管JSON是Javascript的一个子集,但JSON是独立于语言的文本格式,并且采用了类似于C语言家族的一些习惯。JSON解析器和JSON库支持许多不同的编程语言。
cJSON是一个仅有一个.h文件,一个.c文件组成的JSON解析器,它是由纯C(ANSI C89)实现的,跨平台性较好。cJSON中有一个cJSON结构体。cJSON是采用链表存储的。
cjson api创建json文件
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include "cJSON.h"
int main(int argc, const char* argv[])
{
// 创建对象
cJSON* obj = cJSON_CreateObject();
// 创建子对象
cJSON* subObj = cJSON_CreateObject();
// 添加key-value
cJSON_AddItemToObject(subObj, "factory", cJSON_CreateString("一汽大众"));
cJSON_AddItemToObject(subObj, "last", cJSON_CreateNumber(31));
cJSON_AddItemToObject(subObj, "price", cJSON_CreateNumber(83));
cJSON_AddItemToObject(subObj, "sell", cJSON_CreateNumber(49));
cJSON_AddItemToObject(subObj, "sum", cJSON_CreateNumber(80));
// 创建json数组
cJSON* array = cJSON_CreateArray();
// array添加元素
cJSON_AddItemToArray(array, cJSON_CreateNumber(123));
cJSON_AddItemToArray(array, cJSON_CreateBool(1));
cJSON_AddItemToArray(array, cJSON_CreateString("hello, world"));
// 数组中的对象
cJSON* subsub = cJSON_CreateObject();
cJSON_AddItemToObject(subsub, "梅赛德斯奔驰",
cJSON_CreateString("心所向, 持以恒"));
cJSON_AddItemToArray(array, subsub);
cJSON_AddItemToObject(subObj, "other", array);
// obj中添加key - value
cJSON_AddItemToObject(obj, "奔驰", subObj);
// 数据格式化
char* data = cJSON_Print(obj);
FILE* fp = fopen("car.json", "w");
fwrite(data, sizeof(char), strlen(data)+1, fp);
fclose(fp);
return 0;
}
gcc Json_create.c cJSON.c -o create -lm
./create
cjson解析api
#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;
}