Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Re: Missing a decimal point [Thread Next] Re: Missing a decimal pointTo: NULL Date: 2/4/2005 8:01:00 PM
Ivan wrote:
> Basically (Simplified) it looks like this (VB
> sample):
> Dim dom As New MSXML2.DOMDocument
> Dim val As Double
> 'the xmlfile is <value>2.25</value>
> dom.Load "C:\Code\Misc\xmlparser\value.xml"
> val = CDbl(dom.Text) 'this returns 225 if the Decimal separator is (,)
> and the group separator is (.)
> To me the question is is there any way in a pure XML to specify the type
> and maybe the used decimal separator...
So this is more a VB issue then, if the XML contains 2.25 as text then
you need to make sure in your VB program that you have the proper locale
set in VB before you call CDbl. I can't help you with VB but in VBScript
you would do e.g.
Option Explicit
Dim XmlDocument, Loaded, NumberList, NumberElement, NumberString,
NumberValue
Const USLocale = "en-US"
Set XmlDocument = WScript.CreateObject("Msxml2.DOMDocument.4.0")
XmlDocument.async = False
Loaded = XmlDocument.Load("test2005020401.xml")
If Loaded Then
SetLocale USLocale
Set NumberList = XmlDocument.selectNodes("//number")
For Each NumberElement In NumberList
NumberString = NumberElement.text
NumberValue = CDbl(NumberString)
WScript.Echo "Parsed string """ & NumberString & """ as double " &
NumberValue
Next
End If
then with the XML being
<?xml version="1.0" encoding="UTF-8"?>
<numbers>
<number>2.25</number>
</numbers>
the script outputs
Parsed string "2.25" as double 2.25
I am not sure why your program could not do that, of course it probably
needs to store the current locale first and then set it back after
parsing the XML.
If you want to look into XML data typing then XML schema can do that but
I am not sure there is a way to have VBScript or VB CDbl take not of
that. However using XDR data types there is a value nodeTypeValue in the
MSXML DOM so with the XML changed to
<?xml version="1.0" encoding="UTF-8"?>
<numbers xmlns:dt="urn:schemas-microsoft-com:datatypes">
<number dt:dt="float">2.25</number>
</numbers>
and the VBScript changed to
Option Explicit
Dim XmlDocument, Loaded, NumberList, NumberElement, NumberValue, NumberType
Set XmlDocument = WScript.CreateObject("Msxml2.DOMDocument.4.0")
XmlDocument.async = False
Loaded = XmlDocument.Load("test2005020401.xml")
If Loaded Then
Set NumberList = XmlDocument.selectNodes("//number")
For Each NumberElement In NumberList
NumberValue = NumberElement.nodeTypedValue
WScript.Echo "Found value of type " & NumberElement.dataType & "
converted to " & NumberValue & " of VBScript type " &
VarType(NumberValue) & "."
Next
End If
I get the following output:
Found value of type float converted to 2,25 of VBScript type 5.
So the type 5 in VBScript is what is wanted:
vbDouble 5 Double-precision floating-point number
Don't let the "2,25" in the output irritate you, I am on German Windows XP.
Please report back if nodeTypedValue in VB gives you the double value
you are looking for.
--
Martin Honnen
http://JavaScript.FAQTs.com/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
