当前位置: 首页 > 工具软件 > LibXML2 > 使用案例 >

libxml2使用

孙佑运
2023-12-01

参考

https://blog.csdn.net/fanwenjieok/article/details/52848092

 

1.wget http://xmlsoft.org/sources/libxml2-2.9.10.tar.gz
2.
. /autogen.sh
./configure  CROSS_COMPILE=arm-linux-gnueabi --host=arm-linux-gnueabi --prefix=/home/xxx/libxml2 --enable-shared --with-python=no
make && make install

3.例子test.c
cd /home/xxx/libxml2
创建test.c
arm-linux-gnueabi-gcc test.c -o test -I./include/libxml2 -lxml2 -L./lib

xml使用示例https://www.cnblogs.com/lpshou/archive/2013/06/18/3141596.html

 

 

int get_node_value(xmlNodePtr node, char *nodename, char *value, int size)
{
    int i = 0;
    xmlNodePtr curNode;
    xmlChar *szKey;

    
    curNode = node;
    if(curNode == NULL) {
        printf("node is null\n");
        return -1;
    }
    curNode = curNode->xmlChildrenNode;

    while(curNode != NULL) {
        //取出节点中的内容
        if(xmlStrcmp(curNode->name, (const xmlChar *)"text")) {
            //printf("[%s][%d]name<%s>\n", node->name, i++, curNode->name);
        } 
        if(!xmlStrcmp(curNode->name, (const xmlChar *)nodename)) {
            szKey = xmlNodeGetContent(curNode);
            //printf("%s:%s\n", nodename, szKey);
            strncpy(value, szKey, size);
            xmlFree(szKey);
            return 0;
        }
        if(get_node_value(curNode, nodename, value, size) == 0) {
            return 0;
        }
        curNode = curNode->next;
    }

    
    return -1;
}


int parse_xml_buf(char *buf, int buf_size, char *filename, char *key, char *key_value, int size, char *save_file)
{
    xmlDocPtr doc;
    xmlNodePtr curNode;
    int ret = -1;
    
    
    if(filename != NULL) {
        doc = xmlReadFile(filename,"UTF-8",XML_PARSE_RECOVER);
        if(NULL == doc) {
            fprintf(stderr,"Document not parsed successfully\n");    
            return -1;
        }

    } else {
        doc = xmlReadMemory(buf, buf_size, NULL,"UTF-8",XML_PARSE_RECOVER);
        if(NULL == doc) {
            fprintf(stderr,"Document not parsed successfully\n");    
            return ret;
        }
    }

    curNode = xmlDocGetRootElement(doc); 
    if(NULL == curNode) {
        fprintf(stderr,"empty document\n");
        xmlFreeDoc(doc);
        return ret;
    }
    //printf("name<%s>\n", curNode->name);
    bzero(key_value, size);
    if(get_node_value(curNode, key, key_value, size) == 0) {
        //printf("find node value:%s\n", key_value);
        ret = 0;
    }
    xmlFreeDoc(doc);
    return ret;
}

 

 类似资料: