Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Missing a decimal point

From: Martin Honnen <mahotrash@-----.-->
To: 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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent