欢迎小伙伴的点评??,相互学习、互关必回、全天在线🍳🍳🍳 博主将自己研发xml微型服务器,存储方法及读取方法的经验与之分享🌏🌏🙂
前言
本章将给大家带来mxml的概述及简单实例
一、mxml概述
XML(可扩展标记语言)是由W3C推出的网络数据表示、传递和交换的标准,是Internet环境中跨平台的、依赖于数据内容的技术,目前应用日益广泛。XML是eXtensible Markup Language(可扩展标记语言)的缩写, 这是标准的定义。根据其命名可以看出,XML的核心是标记,XML的强大功能来自于扩展性,也即XML是定义标记语言的语言(元语言)。
二、Ubuntu环境下mxml的环境搭配
2.1、mxml官方下载链接如下
mxml下载链接
2.2、解压并安装
打开命令终端输入解压命令解压后如图一所示
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的环境已经安装完毕 路径地址如图四所示 图四
三、原码解析
3.1、编译环境
gedit xmlwrite.c
gedit xmlread.c
- 生成xmlwrite.xml 文件、使用Gcc编译命令如下:
gcc xmlwrite.c -lmxml -lpthread -o create
1、如果发生报错更新一下缓冲命令如下:
sudo ldconfig
2、生成create 此时输入命令:
./create
- 读取xmlwrite.xml 文件 、使用使用Gcc编译命令如下:
gcc xmlread.c -lmxml -lpthread -o read.exe
1、生成 read.exe 此时输入命令:
./read.exe
3.2、创建写入数据文件 xmlwrite.c
#include <stdio.h>
#include <mxml.h>
int main(int argc, const char* argv[])
{
mxml_node_t *xml = mxmlNewXML("1.0");
mxml_node_t* A = mxmlNewElement(xml, "A");
mxml_node_t* B = mxmlNewElement(A, "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 = 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");
FILE* fp = fopen("xmlwrite.xml", "w");
mxmlSaveFile(xml, fp, MXML_NO_CALLBACK);
fclose(fp);
mxmlDelete(xml);
return 0;
}
3.3、创建写入数据文件 xmlread.c
#include <stdio.h>
#include <mxml.h>
int main(int argc, const char* argv[])
{
FILE* fp = fopen("xmlwrite.xml", "r");
if(fp == NULL)
{
printf("fopen error\n");
return 0;
}
mxml_node_t* root = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
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 = mxmlFindElement(B, root, "B", NULL, NULL, MXML_DESCEND);
}
fclose(fp);
mxmlDelete(root);
return 0;
}
3.4、效果实例
图五 图六
四、增添16进制转换为字符串
4.1、编译环境
gedit xmlwrite.c
gedit xmlread.c
- 生成xmlwrite.xml 文件、使用Gcc编译命令如下:
gcc xmlwrite.c -lmxml -lpthread -o create
1、如果发生报错更新一下缓冲命令如下:
sudo ldconfig
2、生成create 此时输入命令:
./create
- 读取xmlwrite.xml 文件 、使用使用Gcc编译命令如下:
gcc xmlread.c -lmxml -lpthread -o read.exe
1、生成 read.exe 此时输入命令:
./read.exe
4.2、xmlwrite.c
#include <stdio.h>
#include <mxml.h>
#include<stdlib.h>
#include<string.h>
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;
}
}
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++;
}
}
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[])
{
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);
mxml_node_t *xml = mxmlNewXML("1.0");
mxml_node_t* A = mxmlNewElement(xml, "A");
mxml_node_t* B = mxmlNewElement(A, "B");
mxml_node_t* C1 = mxmlNewElement(B, "C1");
mxmlNewText(C1, 0, "xmlwrite1");
mxmlElementSetAttr(C1, "xmlwrite1", outstr);
mxml_node_t* C2 = mxmlNewElement(B, "C2");
mxmlNewText(C2, 0, "xmlwrite2");
mxml_node_t* C3 = mxmlNewElement(B, "C3");
mxmlNewText(C3, 0, "xmlwrite3");
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");
FILE* fp = fopen("xmlwrite.xml", "w");
mxmlSaveFile(xml, fp, MXML_NO_CALLBACK);
fclose(fp);
mxmlDelete(xml);
return 0;
}
4.3、xmlread.c
#include <stdio.h>
#include <mxml.h>
#include<stdlib.h>
#include<string.h>
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;
}
}
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++;
}
}
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[])
{
FILE* fp = fopen("xmlwrite.xml", "r");
if(fp == NULL)
{
printf("fopen error\n");
return 0;
}
mxml_node_t* root = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
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"));
int hexlen = str_sw_hex(data_xml, strlen(data_xml), outhex);
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 = mxmlFindElement(B, root, "B", NULL, NULL, MXML_DESCEND);
}
fclose(fp);
mxmlDelete(root);
return 0;
}
4.4、增添16进制后的效果实例
图七 图八
五、总结
本章简要介绍了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文档。
|