libxml2 使用小结

来源:互联网 发布:阿里云购买 编辑:程序博客网 时间:2024/06/12 01:13

libxml2 是用于C/C++的xml编程的,可以解析或者修改xml文件。


xmlNodexml文档中的节点,是一个struct,位于tree.h

typedef struct _xmlNode xmlNode;

typedef xmlNode *xmlNodePtr;

struct _xmlNode {

    void           *_private;/* application data */

    xmlElementType   type;   /* type number, must be second ! */

    const xmlChar   *name;      /* the name of the node, or the entity */

    struct _xmlNode *children; /* parent->childs link */

    struct _xmlNode *last;   /* last child link */

    struct _xmlNode *parent;/* child->parent link */

    struct _xmlNode *next;   /* next sibling link */

    struct _xmlNode *prev;   /* previous sibling link */

    struct _xmlDoc *doc;/* the containing document */

    /* End of common part */

    xmlNs           *ns;        /* pointer to the associated namespace */

    xmlChar         *content;   /* the content */

    struct _xmlAttr *properties;/* properties list */

    xmlNs           *nsDef;     /* namespace definitions on this node */

    void            *psvi;/* for type/PSVI informations */

    unsigned short   line;   /* line number */

    unsigned short   extra; /* extra data for XPath/XSLT */

};

可以用到的方法:

// 以 XML_PARSE_NOBLANKS | XML_PARSE_NODICT 为参数打开,使文档不会出现空格 和 换行问题

    doc = xmlReadFile(szDocName, "utf-8", XML_PARSE_NOBLANKS | XML_PARSE_NODICT);   //解析文档

//遍历所有节点

    while (NULL != curNode)
    {
        if (!xmlStrcmp(curNode->name, (const xmlChar*)"family")) {        //获取名称为family的节点  
            setNode = curNode->xmlChildrenNode;
            while (NULL != setNode) {
                if (!xmlStrcmp(setNode->name, (const xmlChar*)"nameset")) {
                    //xmlNewTextChild(setNode, NULL, (const xmlChar*)"hello", (const xmlChar*)"word");  // 增加节点
                    //nameNode = setNode->xmlChildrenNode; // 得到子节点
                    //xmlUnlinkNode(nameNode);  // 删除节点
                    //xmlFreeNode(nameNode);    // 释放节点
                    nameNode = setNode->xmlChildrenNode;
                    while (NULL != nameNode) {
                        if (!xmlStrcmp(nameNode->name, (const xmlChar*)"name")) {

                           // 得到节点中的content有两种方法,注意要通过本节点的子节点来获得

                            xmlChar* content = xmlNodeListGetString(doc, nameNode->xmlChildrenNode, 1);
                            allName[name_num] = content;
                            name_num++;
                            printf("nameContent = %s\n", nameNode->xmlChildrenNode->content); // 得到节点中的content

                        }

nameNode = nameNode->next;

                    }
                }
                setNode = setNode->next;
            }
        }
        curNode = curNode->next;
    }

//查找属性为name的值的节点  
xmlChar* szPropity = xmlGetProp(propNode, (const xmlChar*)"name");  
if (!xmlStrcmp((const xmlChar*)szPropity, (const xmlChar*)"WLAN_Console"))  
{  
xmlAttrPtr setAttrPtr = propNode->properties;  
                while (NULL != setAttrPtr)  
                {  
                      xmlSetProp(propNode, (const xmlChar*)"priority", (const xmlChar*)"debug");  //设置属性priority的值 
               setAttrPtr = setAttrPtr->next;  
                 }  

}  

//合并文档方法

    curNode = xmlDocGetRootElement(doc); // 得到文档的根节点
    curNode2 = xmlDocGetRootElement(doc2);
    curNode2 = curNode2->xmlChildrenNode;
    while (NULL != curNode2) {
        if (!xmlStrcmp(curNode2->name, (const xmlChar*)"family")) {
            xmlUnlinkNode(curNode2);  
            xmlAddChild(curNode,curNode2); // 添加节点
            curNode2 = curNode2->next;
        }
    }

//使用Xpath得到节点的集合

    xmlDocPtr doc;
    xmlXPathContextPtr xpathCtx;
    xmlXPathObjectPtr xpathObj;
    xmlNodeSetPtr NodeSet;

    doc = xmlReadFile(szDocName, "utf-8", XML_PARSE_NOBLANKS | XML_PARSE_NODICT);
    xpathCtx = xmlXPathNewContext(doc);
    xpathObj = xmlXPathEvalExpression((const xmlChar*)("/family/set/name"), xpathCtx);
    NodeSet = xpathObj->nodesetval;

    NodeSet->nodeTab[i] // nodeTab[i] 是该路径所有节点的集合
    NodeSet->nodeNr  // nodeNr 是节点的总数

//Xpath 可以简化xml的编辑。


0 0
原创粉丝点击