C++ XML解析 使用Mini-XML

C++ XML解析 使用Mini-XML 

先下载Mini-XML, http://www.minixml.org/

编译安装:

./configure --prefix=/foo 

make

make install

然后复制 安装目录的lib中 libmxml.a ;和include到自己的项目中即可使用。

举例: 

<?xml version="1.0" encoding="ISO-8859-1"?>
<drc>
<setting>
    <single_writer>true</single_writer>
    <log_file>data.log</log_file>
</setting>
<host>
    <ip>192.168.1.1</ip>
    <port>3306</port>
    <username>xxx</username>
    <password>xxx</password>
    <database>databasename</database>
</host>
<drc>

C++ 代码:

    FILE *fp;
    mxml_node_t *tree;
    mxml_node_t *pnode;
    mxml_node_t *vnode;
    fp= fopen("drc.xml","r");
    tree = mxmlLoadFile(NULL,fp,MXML_TEXT_CALLBACK);
    pnode = mxmlFindElement(tree,tree,"setting",NULL,NULL,MXML_DESCEND);
    vnode = mxmlFindElement(pnode,pnode,"single_writer",NULL,NULL,MXML_DESCEND);
    cout<<"single_writer:"<<vnode->child->value.text.string<<endl;
    mxmlDelete(tree);
    fclose(fp);

注意解析时,> < 中间的空格也会被解析成一个node; 记得读取完毕后用mxmlDelete(tree);释放资源


你可能感兴趣的:(C++,xml,tree,null,callback,FP)