[xml] question about getting numbers out of an xml file



I've been looking through the archives, and reading the API, and I'm curious about something that must be a simple thing.

I am using a small xml file to store some numbers (ints and floats). I am trying to use an xpath to extract them.

So, I started this way (sort of pseudo C code):

doc      = xmlParseFile(filename);
xpathCtx = xmlXPathNewContext(doc);
xpath    = "/path/to/element";
xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
content  = xmlNodeGetContent(xpathObj->nodesetval->nodeTab[0]);

This gives me the content of the element as an xmlChar* (it works - if it shouldn't then I made a typo). Of course since the element content is to be used as a number (int, float) then I have to further convert it from a string to a number.

So, looking for something more elegant, I came across the function:

        xmlXPathCastToNumber(xpathObj)

This works fine returning a double. But I also came across the function:

        xmlXPathStringEvalNumber(xpathObj->stringval);
                or
        xmlXPathStringEvalNumber(xpathObj->floatval);

and it does not work. What I mean is, it doesn't work for my purposes - it returns null, and herein lies my question. I'm gathering that xmlXPathCastToNumber is essentially the same thing as the pseudo C code; that is, the mechanism to get to the content is via the xmlNodeSetPtr. Whereas, xmlXPathStringEvalNumber does not work because I am accessing that part of the xmlXPathObject structure that has never been filled out... ?

Can someone please discuss this and correct my thinking? How exactly to use xmlXPathStringEvalNumber(), and when/why?


Stefan Jeglinski




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]