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

On Sun, Mar 18, 2007 at 12:55:14AM -0400, Stefan Jeglinski wrote:
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:


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


  Wrong approach ... using the semi-internals functions of XPath module
to do the work instead of using XPath to do the extraction in a portable
way is really not 'elegant' from my point of view

  something like xpath="number(/path/to/element)"
The evaluation will bring back a number , i.e. the xpathObj will be 
different but you will get the float directly.


Red Hat Virtualization group
Daniel Veillard      | virtualization library
veillard redhat com  | libxml GNOME XML XSLT toolkit | Rpmfind RPM search engine

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