gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:43:29 37 4
gpt4 key购买 nike

假设我有以下 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>

我想为我的函数提供以下参数:“CD2.MYPLAYLIST1.pop”作为答案,我想得到以下内容:“数量= 80”我怎样才能赚到这个钱?我想在以下函数中实现它:

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

其中 ptr_node_Parent 是我的根(CATALOG),ptr_src 是我的字符串(“CD2.MYPLAYLIST1.pop”)

我唯一能做的是:

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 函数用于此目的?先感谢您,伴侣

最佳答案

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

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

}

关于c - 使用 libxml2 通过路径获取属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45211018/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com