Re: [xml] question about getting numbers out of an xml file
- From: Daniel Veillard <veillard redhat com>
- To: Stefan Jeglinski <jeglin 4pi com>
- Cc: xml gnome org
- Subject: Re: [xml] question about getting numbers out of an xml file
- Date: Sun, 18 Mar 2007 06:24:35 -0400
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:
xmlXPathCastToNumber(xpathObj)
This works fine returning a double. But I also came across the function:
xmlXPathStringEvalNumber(xpathObj->stringval);
or
xmlXPathStringEvalNumber(xpathObj->floatval);
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
see
http://www.w3.org/TR/xpath#numbers
and
http://www.w3.org/TR/xpath#function-number
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.
Daniel
--
Red Hat Virtualization group http://redhat.com/virtualization/
Daniel Veillard | virtualization library http://libvirt.org/
veillard redhat com | libxml GNOME XML XSLT toolkit http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]