使用libxml2按路径获取属性值?

时间:2017-07-20 09:41:37

标签: c xml libxml2

假设我有以下xml:

 <CATALOG>
    <CD name="CD1">
        <music name="MYPLAYLIST1">
            <style styleName="rock" quantity="120"></style>
            <style styleName="pop" quantity="20"></style>
        </music>
    </CD>
    <CD name="CD2">
        <music name="MYPLAYLIST1">
            <style styleName="rock" quantity="80"></style>
            <style styleName="pop" quantity="80"></style>
        </music>
        <music name="MYPLAYLIST2">
            <style styleName="reggae" ></style>
            <style styleName="rap"></style>
        </music>
    </CD>
</CATALOG>

我希望将以下参数提供给我的函数:&#34; CD2.MYPLAYLIST1.pop&#34; 作为答案,我想得到以下内容:&#34;数量= 80&#34; 我怎么能赚到这个?我想在以下函数中实现它:

void fv_v_getAttr(xmlNode *ptr_Node_Parent,char* ptr_src){
...
}

其中ptr_node_Parent是我的根(CATALOG),ptr_src是我的字符串(&#34; CD2.MYPLAYLIST1.pop&#34;)

我唯一能做的就是:

void fv_v_getAttr(xmlNode *ptr_Node_Parent,char* ptr_src){
    char *ptr_help = strdup(ptr_src);
    ptr_src=strtok(ptr_help,".");

    printf("\CD name:%s\n",ptr_src);
    ptr_src=strtok(NULL,".");
    printf("music name:%s\n", ptr_src);
    ptr_src=strtok(NULL,".");
    printf("stylename:%s\n", ptr_src);;

}

我是否需要检查整个树或者是否有任何libxml2函数? 先感谢您, 伴侣

1 个答案:

答案 0 :(得分:0)

重新考虑问题后,我可以使用以下代码解决问题:

void fv_v_getAttr(xmlNode *ptr_Node_Parent,char* ptr_src){
char *ptr_help = strdup(ptr_src);
xmlNode *ptr_Node_Child = ptr_Node_Parent->children;     
ptr_src=strtok(ptr_help,".");                          

while(ptr_Node_Child){
    xmlAttr *ptr_Attr = ptr_Node_Child->properties;
    if( (ptr_Node_Child->type == XML_ELEMENT_NODE) && (strcmp(xmlGetProp(ptr_Node_Child,ptr_Attr->name),ptr_src) == NULL )){ 
        ptr_Node_Child = ptr_Node_Child->children;         
        ptr_src=strtok(NULL,".");                          
        while(ptr_Node_Child){
            ptr_Attr = ptr_Node_Child->properties;
            if((ptr_Node_Child->type == XML_ELEMENT_NODE) && (strcmp(xmlGetProp(ptr_Node_Child,ptr_Attr->name),ptr_src) == NULL )){ 
                ptr_Node_Child= ptr_Node_Child->children;  
                ptr_src=strtok(NULL,".");    
                while(ptr_Node_Child){
                    ptr_Attr = ptr_Node_Child->properties;
                    xmlAttr *attr_name = ptr_Node_Child->properties;     
                    if((ptr_Node_Child->type == XML_ELEMENT_NODE) && (strcmp(xmlGetProp(ptr_Node_Child,ptr_Attr->name),ptr_src) == NULL )) {
                        attr_name=attr_name->next;                                                        
                        printf("\n%s=%s\n", attr_name->name, xmlGetProp(ptr_Node_Child,attr_name->name)); 
                        ptr_Node_Child = ptr_Node_Child->next;
                    } 
                    else{
                       ptr_Node_Child = ptr_Node_Child->next;
                    }
                }//endWhile
            } 
            else{
                ptr_Node_Child=ptr_Node_Child->next;
            }
        }//endWhile
    }
    else{
        ptr_Node_Child = ptr_Node_Child->next;
    }
}//endWhile  

}