0

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 ?

Tom's
  • 2,448
  • 10
  • 22
  • For clarity: are those backslashes actually in your input line? If they are, then it's not valid XML. If they are not, please make sure your *example* reflects *actual* data, so we can test your code. – Jongware Jan 12 '16 at 09:53
  • Are you referring at the four \" or the final / ? I need to escape those " because if I don't, the string will be incorrect. – Tom's Jan 12 '16 at 09:59
  • "it appear there is only one node". Yeah, the example node, right? The code seems to work as i would expect it to. What do you expect xmlgetnodepath to do. – Millie Smith Jan 12 '16 at 10:02
  • I'm referring to the [backslashes](https://en.wikipedia.org/wiki/Backslash). How will the string be 'incorrect'? Just for the Stack Overflow editor? As it is, it is incorrect XML. – Jongware Jan 12 '16 at 10:03
  • 1
    @Jongware scroll down a little in the code. The example string is in the code. – Millie Smith Jan 12 '16 at 10:05
  • @Jongware Ahh, I understand now ! (thanks Millie Smith). Yeah, this string is in the C Code, sorry ... – Tom's Jan 12 '16 at 10:08
  • @MillieSmith Indeed, the is only one node, and xmlgetnodepath do his job. My problem is I can't retrieve the value "OK" from "status" and "200" from "value". I just do a test with xmlNodeDump and I see as a result. So OK, I can parse manually from this point, but is there a way to say that to libxml2 ? It's important, because after (another code), I will use Xpath in this xmlTree – Tom's Jan 12 '16 at 10:11
  • 1
    Also http://stackoverflow.com/questions/1092227/how-to-get-attributes-from-a-node-in-libxml2-in-iphone-and-in-general – Millie Smith Jan 12 '16 at 10:18
  • @MillieSmith Thanks, that's exactly what i looked for. I didn't know that part of a xml balise is know as "Prop" (i think this is for "property" ?). – Tom's Jan 12 '16 at 10:24
  • Yeah. Prop is a common abbreviation for property. – Millie Smith Jan 12 '16 at 10:32

0 Answers0