![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: Why would encoding="UTF-8" chance to "UTF=16"? >Thread Next - Re: Why would encoding="UTF-8" chance to "UTF=16"? Re: Why would encoding="UTF-8" chance to "UTF=16"?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
| ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
