欢迎小伙伴的点评✨✨,相互学习、互关必回、全天在线🍳🍳🍳
博主将自己研发xml微型服务器,存储方法及读取方法的经验与之分享🌏🌏🙂
本章将给大家带来mxml的概述及简单实例
XML(可扩展标记语言)是由W3C推出的网络数据表示、传递和交换的标准,是Internet环境中跨平台的、依赖于数据内容的技术,目前应用日益广泛。XML是eXtensible Markup Language(可扩展标记语言)的缩写, 这是标准的定义。根据其命名可以看出,XML的核心是标记,XML的强大功能来自于扩展性,也即XML是定义标记语言的语言(元语言)。
打开命令终端输入解压命令解压后如图一所示
tar -xzvf mxml-3.3.1.tar.gz
图一
解压后输入
cd mxml-3.3.1
进入 mxml-3.3.1 文件夹内输入安装命令
./configure
运行完后出现如图二所示
图二
然后输入make指令编译
make
运行完后出现如图三所示
图三
接着输入sudo make install 命令进行安装
sudo make install
此时mxml的环境已经安装完毕 路径地址如图四所示
图四
gedit xmlwrite.c
gedit xmlread.c
gcc xmlwrite.c -lmxml -lpthread -o create
1、如果发生报错更新一下缓冲命令如下:
sudo ldconfig
2、生成create 此时输入命令:
./create
gcc xmlread.c -lmxml -lpthread -o read.exe
1、生成 read.exe 此时输入命令:
./read.exe
#include
#include
int main(int argc, const char* argv[])
{
/*创建xml文件头节点*/
mxml_node_t *xml = mxmlNewXML("1.0");
/*创建xml第一个根节点“A” */
mxml_node_t* A = mxmlNewElement(xml, "A");
/*创建A节点上的子节点“B”*/
mxml_node_t* B = mxmlNewElement(A, "B");
/*在B子节点上加入三个同级的子节点*/
mxml_node_t* C1 = mxmlNewElement(B, "C1");
mxmlNewText(C1, 0, "xmlwrite1");
mxmlElementSetAttr(C1, "xmlwrite1", "String");
mxml_node_t* C2 = mxmlNewElement(B, "C2");
mxmlNewText(C2, 0, "xmlwrite2");
mxml_node_t* C3 = mxmlNewElement(B, "C3");
mxmlNewText(C3, 0, "xmlwrite3");
/*创建子节点B的同级节点“B”*/
/*在第二个B子节点上加入三个同级的子节点*/
B = mxmlNewElement(A, "B");
C1 = mxmlNewElement(B, "C1");
mxmlNewText(C1, 0, "xmlwrite1");
mxmlElementSetAttr(C1, "xmlwrite1", "String");
C2 = mxmlNewElement(B, "C2");
mxmlNewText(C2, 0, "xmlwrite2");
C3 = mxmlNewElement(B, "C3");
mxmlNewText(C3, 0, "xmlwrite3");
/*将xml内容保存到磁盘*/
FILE* fp = fopen("xmlwrite.xml", "w");
mxmlSaveFile(xml, fp, MXML_NO_CALLBACK);
fclose(fp);
mxmlDelete(xml);
return 0;
}
#include
#include
int main(int argc, const char* argv[])
{
/*从磁盘加载xmlwrite.xml文件*/
FILE* fp = fopen("xmlwrite.xml", "r");
if(fp == NULL)
{
printf("fopen error\n");
return 0;
}
/*root 节点指向xml文件头*/
mxml_node_t* root = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
/*遍历 - 取出各个节点的值*/
/*找到第一个B节点*/
mxml_node_t* B = mxmlFindElement(root, root, "B", NULL, NULL, MXML_DESCEND);
if(B == NULL)
{
printf("xml node not found\n");
return 0;
}
while( B )
{
printf("==================\n");
printf("B: \n");
/*向下走一个节点*/
mxml_node_t* node = mxmlWalkNext(B, root, MXML_DESCEND_FIRST);
printf(" xmlwrite = %s\n",mxmlElementGetAttr(node,"xmlwrite1"));
printf(" C1 = %s\n", mxmlGetText(node, NULL));
/*向下走一个节点*/
node = mxmlWalkNext(node, root, MXML_NO_DESCEND);
printf(" C2 = %s\n", mxmlGetText(node, NULL));
/*向下走一个节点*/
node = mxmlWalkNext(node, root, MXML_NO_DESCEND);
printf(" C3 = %s\n", mxmlGetText(node, NULL));
/*搜索下一个B节点*/
B = mxmlFindElement(B, root, "B", NULL, NULL, MXML_DESCEND);
}
fclose(fp);
mxmlDelete(root);
return 0;
}
图五
图六
gedit xmlwrite.c
gedit xmlread.c
gcc xmlwrite.c -lmxml -lpthread -o create
1、如果发生报错更新一下缓冲命令如下:
sudo ldconfig
2、生成create 此时输入命令:
./create
gcc xmlread.c -lmxml -lpthread -o read.exe
1、生成 read.exe 此时输入命令:
./read.exe
#include
#include
#include
#include
/*******************************/
static unsigned char byte_char_sw_hex( unsigned char ucCharacter )
{
if( ( ucCharacter >= '0' ) && ( ucCharacter <= '9' ) )
{
return ( unsigned char )( ucCharacter - '0' );
}
else if( ( ucCharacter >= 'a' ) && ( ucCharacter <= 'f' ) )
{
return ( unsigned char )( ucCharacter - 'a' + 0x0A );
}
else if( ( ucCharacter >= 'A' ) && ( ucCharacter <= 'F' ) )
{
return ( unsigned char )( ucCharacter - 'A' + 0x0A );
}
else
{
return 0xFF;
}
}
/*
函数功能:十六进制转换为字符串
org:输入的十六进制数组
len:数组长度
out:返回字符串
*/
void hex_sw_str(unsigned char *org, int len, unsigned char *out)
{
int i =0;
int idx=0;
char str[]="0123456789ABCDEF";
while(i<len) {
unsigned char tmp[2] = "";
tmp[0] = str[org[i]/16];
tmp[1] = str[org[i]%16];
memcpy(out+idx, tmp, 2);
idx+=2;
i++;
}
}
/*
函数功能:十六进制字符串转为十六进制
org:输入的十六进制字符串数组
len:数组长度
out:返回结果
return:返回转换后的长度
*/
int str_sw_hex(unsigned char *org, int len, unsigned char *out)
{
int idx = 0, i = 0;
int l = len%2?(len-1):len;
while(i<l) {
out[idx] = byte_char_sw_hex(org[i])*16;i++;
out[idx] +=byte_char_sw_hex(org[i]);
i++;
idx++;
}
if(len%2) {
out[idx++]=byte_char_sw_hex(org[i]);
}
return idx;
}
/********************************/
int main(int argc, const char* argv[])
{
/*声明16进制的数组*/
unsigned char outstr[200]="";
unsigned char testbuf_hex[]={0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x11,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
hex_sw_str(testbuf_hex, sizeof(testbuf_hex), outstr); /*调用转换函数,使16进制数组转换成字符串*/
/*创建xml文件头节点*/
mxml_node_t *xml = mxmlNewXML("1.0");
/*创建xml第一个根节点“A” */
mxml_node_t* A = mxmlNewElement(xml, "A");
/*创建A节点上的子节点“B”*/
mxml_node_t* B = mxmlNewElement(A, "B");
/*在B子节点上加入三个同级的子节点*/
mxml_node_t* C1 = mxmlNewElement(B, "C1");
mxmlNewText(C1, 0, "xmlwrite1");
mxmlElementSetAttr(C1, "xmlwrite1", outstr); /*将16进制转换的字符串写如xml文件中*/
mxml_node_t* C2 = mxmlNewElement(B, "C2");
mxmlNewText(C2, 0, "xmlwrite2");
mxml_node_t* C3 = mxmlNewElement(B, "C3");
mxmlNewText(C3, 0, "xmlwrite3");
/*创建子节点B的同级节点“B”*/
/*在第二个B子节点上加入三个同级的子节点*/
B = mxmlNewElement(A, "B");
C1 = mxmlNewElement(B, "C1");
mxmlNewText(C1, 0, "xmlwrite1");
mxmlElementSetAttr(C1, "xmlwrite1", outstr);
C2 = mxmlNewElement(B, "C2");
mxmlNewText(C2, 0, "xmlwrite2");
C3 = mxmlNewElement(B, "C3");
mxmlNewText(C3, 0, "xmlwrite3");
/*将xml内容保存到磁盘*/
FILE* fp = fopen("xmlwrite.xml", "w");
mxmlSaveFile(xml, fp, MXML_NO_CALLBACK);
fclose(fp);
mxmlDelete(xml);
return 0;
}
#include
#include
#include
#include
/*************************************/
static unsigned char byte_char_sw_hex( unsigned char ucCharacter )
{
if( ( ucCharacter >= '0' ) && ( ucCharacter <= '9' ) )
{
return ( unsigned char )( ucCharacter - '0' );
}
else if( ( ucCharacter >= 'a' ) && ( ucCharacter <= 'f' ) )
{
return ( unsigned char )( ucCharacter - 'a' + 0x0A );
}
else if( ( ucCharacter >= 'A' ) && ( ucCharacter <= 'F' ) )
{
return ( unsigned char )( ucCharacter - 'A' + 0x0A );
}
else
{
return 0xFF;
}
}
/*
函数功能:十六进制转换为字符串
org:输入的十六进制数组
len:数组长度
out:返回字符串
*/
void hex_sw_str(unsigned char *org, int len, unsigned char *out)
{
int i =0;
int idx=0;
char str[]="0123456789ABCDEF";
while(i<len) {
unsigned char tmp[2] = "";
tmp[0] = str[org[i]/16];
tmp[1] = str[org[i]%16];
memcpy(out+idx, tmp, 2);
idx+=2;
i++;
}
}
/*
函数功能:十六进制字符串转为十六进制
org:输入的十六进制字符串数组
len:数组长度
out:返回结果
return:返回转换后的长度
*/
int str_sw_hex(unsigned char *org, int len, unsigned char *out)
{
int idx = 0, i = 0;
int l = len%2?(len-1):len;
while(i<l) {
out[idx] = byte_char_sw_hex(org[i])*16;i++;
out[idx] +=byte_char_sw_hex(org[i]);
i++;
idx++;
}
if(len%2) {
out[idx++]=byte_char_sw_hex(org[i]);
}
return idx;
}
/***********************************/
int main(int argc, const char* argv[])
{
/*从磁盘加载xmlwrite.xml文件*/
FILE* fp = fopen("xmlwrite.xml", "r");
if(fp == NULL)
{
printf("fopen error\n");
return 0;
}
/*root 节点指向xml文件头*/
mxml_node_t* root = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
/*遍历 - 取出各个节点的值*/
/*找到第一个B节点*/
mxml_node_t* B = mxmlFindElement(root, root, "B", NULL, NULL, MXML_DESCEND);
if(B == NULL)
{
printf("xml node not found\n");
return 0;
}
while( B )
{
printf("==================\n");
printf("B: \n");
/*向下走一个节点*/
mxml_node_t* node = mxmlWalkNext(B, root, MXML_DESCEND_FIRST);
unsigned char data_xml[200];
unsigned char outhex[200]="";
strcpy(data_xml,mxmlElementGetAttr(node,"xmlwrite1")); //将从xml文件读取到的字符串拷贝到数组data_xml中
int hexlen = str_sw_hex(data_xml, strlen(data_xml), outhex); //调用将字符串转换成16进制函数,并将其逐一打印到终端
int length = strlen(data_xml);
printf(" xmlwrite ");
for(int i=0;i<hexlen;i++) {
printf("%02x ", outhex[i]&0xff);
}
printf("\n");
printf(" C1 = %s\n", mxmlGetText(node, NULL));
/*向下走一个节点*/
node = mxmlWalkNext(node, root, MXML_NO_DESCEND);
printf(" C2 = %s\n", mxmlGetText(node, NULL));
/*向下走一个节点*/
node = mxmlWalkNext(node, root, MXML_NO_DESCEND);
printf(" C3 = %s\n", mxmlGetText(node, NULL));
/*搜索下一个B节点*/
B = mxmlFindElement(B, root, "B", NULL, NULL, MXML_DESCEND);
}
fclose(fp);
mxmlDelete(root);
return 0;
}
图七
图八
本章简要介绍了XML的来源、发展和应用,以及XML的特点和相关技术。主要内容如下。
(1) XML是可扩展标记语言,是通用标记语言SGML的一个子集。
(2) XML的主要特点是其良好的可扩展性和XML数据内容与显示样式的分离。
(3) XML的作用,一是作为元标记语言,定义各种实例标记语言标准;二是作为数据标准描述和交换语言,作为存储数据和交换数据的工具。目前许多应用程序都支持XML,特别是DBMS。
(4) XML文档用于描述数据及其结构,在表现时需要CSS或XSL样式语言描述表现样式。
(5) DOM是W3C制定的供应用程序访问XML的标准接口规范,是一种文档对象模型。
(6) XLink是XML链接语言,功能比HTML链接更强大。
(7) XPath是XML路径语言,用来确定XML文档中节点的位置。
(8) XPointer是XML指针语言,是在XPath的基础上,提供一种比XPath更精确的定位方法,允许基于元素类型、属性值、字符内容和相对位置等访问XML文档。