昨天简单写了在VxWorks平台应用mini-XML库解析xml文件,以及怎样用。
https://blog.csdn.net/jianzhao6205/article/details/81638197
今天将根据函数手册写一个例程,主要功能为:使用库函数生成一个xml树保存到user.xml文件,注意fopen()函数文件路径的写法("host:d:\\Tornado2.2\\XML\\user.xml"),对生成的文件进行解析并打印。很简单的一个小程序
#include
#include
#include
#include
#include"D:\Tornado2.2\XML\xmlTest\src\vxw5\config.h"
#include"D:\Tornado2.2\XML\xmlTest\src\mxml.h"
int vmain()
{
FILE *fp;
FILE *fptra;
mxml_node_t *tree;
mxml_node_t *xml; /* */
mxml_node_t *data; /* */
mxml_node_t *node; /* */
mxml_node_t *group; /* */
/* 创建一个新的文档的版本号 */
xml = mxmlNewXML("1.0");
data = mxmlNewElement(xml, "data");
node = mxmlNewElement(data, "node");
mxmlElementSetAttrf(node, "id", "0x01");
mxmlElementSetAttrf(node, "msg", "This is 0x01");
node = mxmlNewElement(data, "node");
mxmlElementSetAttrf(node, "id", "0x02");
mxmlElementSetAttrf(node, "msg", "This is 0x02");
node = mxmlNewElement(data, "node");
mxmlElementSetAttrf(node, "id", "0x03");
mxmlElementSetAttrf(node, "msg", "This is 0x03");
group = mxmlNewElement(data, "group");
node = mxmlNewElement(group, "node");
mxmlElementSetAttrf(node, "id", "0x04");
mxmlElementSetAttrf(node, "msg", "This is 0x04");
node = mxmlNewElement(group, "node");
mxmlElementSetAttrf(node, "id", "0x05");
mxmlElementSetAttrf(node, "msg", "This is 0x05");
node = mxmlNewElement(data, "node");
mxmlElementSetAttrf(node, "id", "0x06");
mxmlElementSetAttrf(node, "msg", "This is 0x06");
node = mxmlNewElement(data, "node");
mxmlElementSetAttrf(node, "id", "0x07");
mxmlElementSetAttrf(node, "msg", "This is 0x07");
/* 创建一个文件并将生成的xml保持存到文件中,注意保存路径是已经存在的 */
fp = fopen("host:d:\\Tornado2.2\\XML\\user.xml", "w");
mxmlSaveFile(xml, fp, MXML_NO_CALLBACK);
fclose(fp);
mxmlDelete(xml);
fptra = fopen("host:d:\\Tornado2.2\\XML\\user.xml", "r");
tree = mxmlLoadFile(NULL, fptra, MXML_TEXT_CALLBACK);
fclose(fptra);
/* 遍历上述创建的文档,并将所有的属性和值打印 */
for (node = mxmlFindElement(tree, tree, "node", NULL, NULL, MXML_DESCEND);
node != NULL;
node = mxmlFindElement(node, tree, "node", NULL, NULL, MXML_DESCEND))
{
printf("id = %s, msg = %s \n",
mxmlElementGetAttr( node, "id"), mxmlElementGetAttr( node, "msg"));
}
printf("\n");
return 0 ;
}
最终生成的xml文件
用到的资源在这里 https://download.csdn.net/download/jianzhao6205/10601698
如果有没有分的可以私信我,我看到了会发给你的,不为赚积分,共同学习