简体   繁体   English

使用libxml2通过路径获取属性值?

[英]Get attribute value by path with libxml2?

Let's assume that I have the following xml: 假设我有以下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>

and I want to give the following parameters to my function: "CD2.MYPLAYLIST1.pop" As the answer I want to get the following: "quantity=80" How Can I earn this? 我想给函数提供以下参数:“ CD2.MYPLAYLIST1.pop”作为答案,我想得到以下内容:“ quantity = 80”我怎么能赚到这个? I want to make it in the following function: 我想通过以下功能实现它:

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

where the ptr_node_Parent is my root (CATALOG), and the ptr_src is my string ("CD2.MYPLAYLIST1.pop") 其中ptr_node_Parent是我的根目录(CATALOG),而ptr_src是我的字符串(“ CD2.MYPLAYLIST1.pop”)

The only thing I could make is: 我唯一能做的是:

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);;

}

Do I need to check the whole tree or is there any libxml2 function for this? 我需要检查整个树还是为此有任何libxml2函数? Thank you in advance, Mate 预先谢谢你,伴侣

After reconsidering the problem, I could solve it with the following code: 重新考虑问题后,我可以使用以下代码解决它:

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  

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM