7

我正在使用解析器从 XML 文件中获取数据。我正在使用 libxml2 来提取数据。我无法从节点获取属性。我只发现nb_attributes得到属性的计数。

4

6 回答 6

14

我认为joostk的意思是attribute->children,给出这样的东西:

xmlAttr* attribute = node->properties;
while(attribute)
{
  xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1);
  //do something with value
  xmlFree(value); 
  attribute = attribute->next;
}

看看这是否适合你。

于 2009-11-06T22:17:02.730 回答
9

如果您只想要一个属性,请使用xmlGetPropxmlGetNsProp

于 2011-07-06T22:36:10.903 回答
4

我想我找到了为什么你只有 1 个属性(至少它发生在我身上)。

问题是我读取了第一个节点的属性,但下一个是文本节点。不知道为什么,但是 node->properties 给了我一个对内存不可读部分的引用,所以它崩溃了。

我的解决方案是检查节点类型(元素为 1)

我正在使用阅读器,所以:

xmlTextReaderNodeType(reader)==1

您可以从http://www.xmlsoft.org/examples/reader1.c获取整个代码并添加它

xmlNodePtr node= xmlTextReaderCurrentNode(reader);
if (xmlTextReaderNodeType(reader)==1 && node && node->properties) {
    xmlAttr* attribute = node->properties;
    while(attribute && attribute->name && attribute->children)
    {
      xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1);
      printf ("Atributo %s: %s\n",attribute->name, value);
      xmlFree(value);
      attribute = attribute->next;
    }
}

到第 50 行。

于 2013-12-07T22:03:54.693 回答
1

尝试类似:

xmlNodePtr node; // Some node
NSMutableArray *attributes = [NSMutableArray array];

for(xmlAttrPtr attribute = node->properties; attribute != NULL; attribute = attribute->next){
    xmlChar *content = xmlNodeListGetString(node->doc, attribute->children, YES);
    [attributes addObject:[NSString stringWithUTF8String:content]];
    xmlFree(content);
}
于 2009-09-23T14:58:22.997 回答
0

如果你使用 SAX 方法 startElementNs(...),这个函数就是你要找的:

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("atrName", attributes, nb_attributes);
// do your magic
free(value);
于 2014-09-05T12:21:52.807 回答
0

我发现使用 libxml2(通过 C++ 中的 libxml++)最简单的方法是使用这些eval_to_XXX方法。它们计算 XPath 表达式,因此您需要使用@property语法。

例如:

std::string get_property(xmlpp::Node *const &node) {
    return node->eval_to_string("@property")
}
于 2015-12-12T20:45:53.470 回答