xml保存数据

一、安装libxml2

1、ubuntu安装:Sudo apt-get install Libxml2

2、交叉编译:下载libxml2库并解压,地址:http://xmlsoft.org/sources/libxml2-sources-2.7.8.tar.gz

./configure --host=mipsel-linux --prefix=/opt/libxml2

Make

Make install

编译程序时使用命令:mipsel-linux-gcc test.c -o test -I/opt/libxml2/include/libxml2/ -L/opt/libxml2/lib -lxml2


二、xml保存数据

init.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<root>
        <test>
                <testone></testone>
                <testtwo>second</testtwo>
                <testthree>first</testthree>
        </test>
        <reference>
                <referenceone>aaa</referenceone>
        </reference>
</root>


test.c文件:

#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

struct testdata{
        char testone[20];
        char testtwo[20];
        char testthree[20];
};

int xml_parse(struct testdata testsave)
{
    xmlDocPtr doc;           //定义解析文档指针
    xmlNodePtr curNode,detail;      //定义结点指针(你需要它为了在各个结点间移动)
    xmlChar *szKey;          //临时字符串变量
    char *szDocName;
    szDocName = "init.xml";
    doc = xmlReadFile(szDocName,"UTF-8",XML_PARSE_RECOVER); //解析文件
    if (NULL == doc)
    {
       fprintf(stderr,"Document not parsed successfully. \n");
       return -1;
    }
    curNode = xmlDocGetRootElement(doc); //确定文档根元素

    xmlNodePtr propNodePtr = curNode;
    while(curNode != NULL)
    {
                if(xmlStrcasecmp(curNode->name,BAD_CAST"test")==0){
                        for(detail=curNode->children;detail;detail=detail->next){
                                if(xmlStrcasecmp(detail->name,BAD_CAST"testone")==0)
                                {
                                        xmlNodeSetContent(detail, BAD_CAST testsave.testone);
                                }
                                else if(xmlStrcasecmp(detail->name,BAD_CAST"testtwo")==0)
                                {
                                        xmlNodeSetContent(detail, BAD_CAST testsave.testtwo);
                                }
                                else if(xmlStrcasecmp(detail->name,BAD_CAST"testthree")==0)
                                {
                                        xmlNodeSetContent(detail, BAD_CAST testsave.testthree);
                                }
                        }
                }
                curNode = curNode->next;
        }

        int nRel = xmlSaveFile("init.xml",doc);
        xmlFreeDoc(doc);

        return 1;
}

int main(void)
{
        struct testdata testsave={"first","second","third"};
        xml_parse(testsave);
        return 0;
}


编译test.c:gcc test.c -o test -lxml2

执行test后,init.xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
        <test>
                <testone>first</testone>
                <testtwo>second</testtwo>
                <testthree>third</testthree>
        </test>
        <reference>
                <referenceone>aaa</referenceone>
        </reference>
</root>



猜你喜欢

转载自blog.csdn.net/NoBack7/article/details/7926204