How to get attributes from node in libxml2

I am working on a parser to retrieve data from an XML file. I am using libxml2 to retrieve data. I cannot get attributes from nodes. I found nb_attributes to get the number of attributes.

+6
c libxml2
source share
6 answers

I think joostk meant the attribute-> children, giving something like this:

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

See if this works for you.

+10
source share

if you need only one attribute use xmlGetProp or xmlGetNsProp

+6
source share

I think I found why you only have 1 attribute (at least it happened to me).

The problem was that I read the attributes for the first node and then the text node. I don’t know why, but the node β†’ properties give me a link to the unreadable part of the memory, so it crashed.

My solution was to check the type of node (element is 1)

I use the reader, therefore:

 xmlTextReaderNodeType(reader)==1 

All the code you can get from http://www.xmlsoft.org/examples/reader1.c and add this

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

to line 50.

+2
source share

Try something like:

 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); } 
+1
source share

If you use the SAX method startElementNs (...), this function is what you are looking for:

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

Using:

 char * value = getAttributeValue("atrName", attributes, nb_attributes); // do your magic free(value); 
0
source share

The easiest way I found using libxml2 (via libxml ++ in C ++) was to use the eval_to_XXX methods. They evaluate the XPath expression, so you need to use the @property syntax.

For example:

 std::string get_property(xmlpp::Node *const &node) { return node->eval_to_string("@property") } 
0
source share

All Articles