虽然这个mini-XML库不是很难,但是还是折腾了几天,其中fopen()函数打开文件的路径是个问题,关于这个讲解很少被提到(也许大神们不屑于讨论路径问题了,或者我太弱)
https://blog.youkuaiyun.com/B_H_L/article/details/41659327
这个博客已经有一个例子了,但是运行有问题再次基础上进行修改,怕以后我自己也忘了
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include <fcntl.h>
#include "D:\Tornado2.2\XML\xmlTest\src\vxw5\config.h"
#include"D:\Tornado2.2\XML\xmlTest\src\mxml.h"
#define VXWORKS
int vmain()
{
FILE *fp;
mxml_node_t *tree,*node;
mxml_node_t *id,*password;
fp = fopen("host:d:\\Tornado2.2\\XML\\debug_settings.xml", "r");
tree = mxmlLoadFile(NULL, fp,MXML_TEXT_CALLBACK);
fclose(fp);
node = mxmlFindElement(tree, tree, NULL, NULL, NULL,MXML_DESCEND);
printf(" year:%s \n",mxmlElementGetAttr(node,"year"));
printf(" date:%s \n",mxmlElementGetAttr(node,"date"));
printf(" month:%s \n",mxmlElementGetAttr(node,"month"));
id = mxmlFindElement(node, tree, "id",NULL, NULL,MXML_DESCEND);
printf("[%s}\n",id->child->value.text.string);
password = mxmlFindElement(node, tree, "password",NULL, NULL,MXML_DESCEND);
printf("[%s]\n",password->child->value.text.string);
mxmlDelete(tree);
return 0 ;
}
fp = fopen("host:d:\\Tornado2.2\\XML\\debug_settings.xml", "r");使用Tornado的VxSim进行模拟读取文件的路径开头host,路径使用双斜杠 “ \\ ”,要解析的xml源码如下所示,解析效果
<?xml version="1.0" encoding="gb2312" ?>
<note year="55" date="33" month="22">
<id>5000</id>
<password>FE-D0-18-00</password>
</note>
mini-XML库文件中在src路径下有config.h文件,其余部分删除只剩下#include "vxw5/config.h"即可,因为在文件中有一个文件夹vxw5包含一个真正的config.h文件。