在libXML sax解析器(C ++)中获取属性值的正确方法是什么? [英] What is the right way to get attribute value in libXML sax parser (C++)?

查看:80
本文介绍了在libXML sax解析器(C ++)中获取属性值的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用libXML的SAX接口来用C ++编写XML解析器应用程序.

I am using the SAX interface of libXML to write a XML parser application in C++.

<abc value="xyz &quot;pqr&quot;"/>

如何解析此属性?

我尝试使用

void startElementNsSAX2Func(void * ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar ** namespaces, int nb_attributes, int nb_defaulted, const xmlChar ** attributes)

,增加属性参数(并检查"以指示属性值的结尾).

, incrementing attributes parameter( and checking for a " to indicate the end of the attribute value).

它适用于属性值中出现的除*&quot;*以外的所有其他属性.

It works for all the attributes other than *&quot;* appearing in the attribute value.

解析这类属性值的正确方法是什么?

What is the right method to parse these kind of attribute values?

谢谢

推荐答案

尝试此功能:

xmlChar *getAttributeValue(char *name, const xmlChar ** attributes,
           int nb_attributes)
{
int i;
const int fields = 5;    /* (localname/prefix/URI/value/end) */
xmlChar *value;
size_t size;
for (i = 0; i < nb_attributes; i++) {
    const xmlChar *localname = attributes[i * fields + 0];
    const xmlChar *prefix = attributes[i * fields + 1];
    const xmlChar *URI = attributes[i * fields + 2];
    const xmlChar *value_start = attributes[i * fields + 3];
    const xmlChar *value_end = attributes[i * fields + 4];
    if (strcmp((char *)localname, name))
        continue;
    size = value_end - value_start;
    value = (xmlChar *) malloc(sizeof(xmlChar) * size + 1);
    memcpy(value, value_start, size);
    value[size] = '\0';
    return value;
}
return NULL;
}

您可以这样使用它:

char *value = getAttributeValue("value", nb_attributes, attributes);
printf("%s\n", value);
free(value);

这篇关于在libXML sax解析器(C ++)中获取属性值的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆