Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Why would encoding="UTF-8" chance to "UTF=16"?

From: "Anthony Jones" <Ant@------------.--->
To: NULL
Date: 7/21/2007 12:30:00 PM


"Hennie" <ze1@h...> wrote in message
news:yr6oi.1508$gn1.647@r......
> Good,
>
> Here is one of the code I am using to transform the file.  The other one
> also use MSXML.DOMDocument and I have tried both.  I found both on the
> Internet.
>
> I hope this helps.
>
> Thank you.
>
> Hennie
>
>
> Sub xml4()
>
> Dim xmlSource
>
> Dim xmlXForm
>
> Dim strErr
>
> Dim strResult
>
>
>
> Dim fso, file
>
> Dim strPath
>
> Const ForWriting = 2
>
>
>
> Set xmlSource = CreateObject("MSXML.DOMDocument")
>
> Set xmlXForm = CreateObject("MSXML.DOMDocument")
>
>
>
> xmlSource.validateOnParse = True
>
> xmlXForm.validateOnParse = True
>
> xmlSource.async = False
>
> xmlXForm.async = False
>
>
>
> 'initializing, and creating the objects to load our XML and XSL files
needed
> in the next step:
>
>
>
>
>
> ' This loads the text that I want to transform
>
> xmlSource.Load "H:\Chem\DATA\XML Report\sample.xml"
>
>
>
>
>
> If Err.Number <> 0 Then
>
> strErr = Err.Description & vbCrLf
>
> strErr = strErr & xmlSource.parseError.reason & " line: " & _
>
> xmlSource.parseError.Line & " col: " & _
>
> xmlSource.parseError.linepos & _
>
> " text: " & xmlSource.parseError.srcText
>
> MsgBox strErr, vbCritical, "Error loading the XML"
>
>
>
> End If
>
>
>
> ' This loads the XSLT transform
>
> 'Here you need to update your Xsl file
>
>
>
> 'xmlXForm.Load "Your XSL file Path"
>
> ' This loads the XSLT transform
>
> xmlXForm.Load "H:\Chem\DATA\XML Report\mews.xsl"
>
> If Err.Number <> 0 Then
>
>
>
> strErr = Err.Description & vbCrLf
>
> strErr = strErr & xmlSource.parseError.reason & " line: " & _
>
> xmlSource.parseError.Line & " col: " & _
>
> xmlSource.parseError.linepos & _
>
> " text: " & xmlSource.parseError.srcText
>
> MsgBox strErr, vbCritical, "Error loading the Transform"
>
> End If
>
>
>
> 'This tells the XML objects to load the files into memory for the
transform
> which we perform next:
>
>
>
> ' This transforms the data in xmlSource
>
> strResult = xmlSource.transformNode(xmlXForm)
>
>
>
> If Err.Number <> 0 Then
>
>
>
> strErr = Err.Description & vbCrLf
>
>
>
> strErr = strErr & xmlSource.parseError.reason & _
>
> " line: " & xmlSource.parseError.Line & _
>
> " col: " & xmlSource.parseError.linepos & _
>
> " text: " & xmlSource.parseError.srcText
>
>
>
> MsgBox strErr, vbCritical, "Error executing the Transform"
>
>
>
> End If
>
>
>
> 'At this point, the transformed XML is placed into a string variable
called
> strResult that we need to write as file to disk:
>
>
>
>
>
> Set fso = CreateObject("Scripting.FileSystemObject")
>
>
>
> 'Here you need to update your Output files
>
> strPath = "H:\Chem\DATA\XML Report\result.xml"
>
>
>
> ' open the file
>
> Set file = fso.opentextfile(strPath, ForWriting, True)
>
>
>
> ' write the info to the file
>
> file.Write strResult
>
>
>
>
>
> End Sub
>

The problem is that all strings in VBScript are unicode.  As soon as you
retrieve the XML content using the xml property the encoding returned has to
be UTF-16 regardless of what you wanted it to be.  What you need to do is
cut out the FileSystemObject, it is unnecessary.  Instead use the
DOMDocument save method and give it the file path.  The DOMDocument will
save the file according to the encoding set in the ?xml declaration and if
one isn't set it will default to UTF-8.

BTW,  The DOMDocument does not throw an error when a document fails to load
or parse hence Err.Number and be 0 whilst xmlSource.parseError contains an
error.  Load returns true when the load is successful and false when there
is a problem.


-- 
Anthony Jones - MVP ASP/ASP.NET




transparent
Print
Mail
Digg
delicious
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