I try to do something that appears to me as really simple : I have this line
<example status="OK" value="200"/>
and i want to parse it with libxml2 and get the value of "status" and "value", but all my try failed.
Here my actual code :
#include <libxml/tree.h>
#include <libxml/parser.h>
void Display_Node(xmlDocPtr doc, xmlNodePtr node)
{
// printf("test :: %s\n", xmlNodeListGetString(doc, noeud, 1));
// printf("test :: %s\n", xmlNodeListGetString(doc, noeud->children, 1));
printf("%2d :: ", node->type);
if (node->type == XML_ELEMENT_NODE) {
xmlChar *chemin = xmlGetNodePath(node);
if (node->children && node->children->type == XML_TEXT_NODE) {
xmlChar *contenu = xmlNodeGetContent(node);
printf("%s -> %s\n", chemin, contenu);
xmlFree(contenu);
} else {
printf("%s\n", chemin);
}
xmlFree(chemin);
} else {
printf("NOTHING\n");
}
// printf("\n");
}
void MyXml_Dump(xmlDocPtr doc, xmlNodePtr node)
{
for (xmlNode *tmp = node; tmp; tmp = tmp->next) {
Display_Node(doc, tmp);
if (tmp->children) {
MyXml_Dump(doc, tmp->children);
}
}
}
int main(void)
{
char *xml = "<example status=\"OK\" value=\"200\"/>";
xmlDocPtr doc = xmlParseMemory(xml, strlen(xml));
if (!doc) {
printf("Error while parsing.\n");
return (1);
}
xmlNodePtr racine = xmlDocGetRootElement(doc);
if (!racine) {
fprintf(stderr, "No xml content.\n");
xmlFreeDoc(doc);
return (1);
}
printf("Xml Dump :\n");
MyXml_Dump(doc, racine);
printf("\n");
xmlFreeDoc(doc);
}
My result is this :
Xml Dump :
1 :: /example
I try to use xmlNodeListGetString or xmlNodeGetContent in order to dump the value I want, but that doesn't work (same result for both).
It seems that the value simply aren't here (because I add a printf in every node and it appear there is only one node), so I begin to wonder if I use the correct parsing function (xmlParseMemory) or if this kind of value is ignored by libxml2.
It's not a library that I use frequently, so i'm a little lost. Is there a way to perform what I want ?