-1

I have a XML that I read with an object of type Msxml2.DOMDocument.4.0. In this XML I receive characters like ë. When I read this character it comes in VBScript code like this . The XML encoding is set to UTF-8.

This is one part of the XML that I receive:

<?xml version="1.0" encoding="utf-8"?>

<shiporder>

  <DriverData>

    <DriverLicenseCountry>Australië</DriverLicenseCountry>

  </DriverData>

</shiporder>

And this is the vbScript code that I use for creating the object:

Set oXml = Server.CreateObject("Msxml2.DOMDocument.4.0")

I read the node like this :

Function GetXMLval2(oDoc, sNoeud)
   Dim oNoeud
   Dim objNode
   Dim colNodes
   Dim sRes

   sRes=""

   Set colNodes=oDoc.selectNodes(sNoeud)

   For Each objNode in colNodes
       sRes= objNode.Text 
   Next

   GetXMLval2=sRes

End Function

Where oDOC is the XML and sNoeud is the node name. The objNode.Txt is returning that weird character.

Any ideas ?

user692942
  • 16,398
  • 7
  • 76
  • 175
Sebastian
  • 617
  • 2
  • 10
  • 30
  • 2
    "I read the node like this" - and do what with it? We can't tell where you're displaying/viewing the character. – Jon Skeet Jul 01 '16 at 09:41
  • @JonSkeet doesn't matter what i do with it. I can send it to the DB, display it or store it in a variable. The problem is that Node.text returns that string with that weird character. – Sebastian Jul 01 '16 at 09:44
  • 2
    It absolutely matters what you do with it, because we don't know where you're seeing that character. If you're trying to display it somewhere that doesn't support the *actual* character, that would explain the problem. Are you able to read the XML file (including the problematic character) correctly from other platforms, e.g. with .NET? – Jon Skeet Jul 01 '16 at 09:45
  • @Sebastian Is this a standalone VBScript or running in Classic ASP? The issue here regardless is encoding mismatch, you are processing a UTF-8 encoded file with something other then UTF-8 hence the weird character. – user692942 Jul 01 '16 at 09:45
  • 1
    @lankymart in this part it is standalone vbscript code. Do you have any ideas how can i set that encoding in vbscript or how can i solve this ? I am using this to make a select in the database. Thank you – Sebastian Jul 01 '16 at 09:56
  • 1
    @Sebastian If this is a standalone script, why is `Server.CreateObject()` used as that is Classic ASP syntax? VBScript uses `CreateObject()` otherwise running this would result in `Microsoft VBScript runtime error: Object required: 'Server'` in `cscript.exe` or `wscript.exe`. – user692942 Jul 01 '16 at 10:41
  • @Lankymart sorry, my bad. That code is written in an ASP page which is included in another ASP page. – Sebastian Jul 01 '16 at 11:35
  • 1
    @Sebastian Well that changes things quite a bit. Take a look [here](http://stackoverflow.com/a/21914278/692942). – user692942 Jul 01 '16 at 11:35
  • 1
    Possible duplicate of [Convert UTF-8 String Classic ASP to SQL Database](http://stackoverflow.com/questions/21866225/convert-utf-8-string-classic-asp-to-sql-database) – user692942 Jul 01 '16 at 11:40

1 Answers1

1

Not entirely sure what the issue here is, as @JonSkeet point's out you haven't explained "where you're seeing that character".

With that in mind did a quick test in cscript.exe

Option Explicit
Dim xml: Set xml = CreateObject("Msxml2.DOMDocument.6.0")
Call xml.Load("test50.xml")

Dim node: Set node = xml.selectSingleNode("//DriverLicenseCountry")

WScript.Echo node.Text

My test uses Msxml2.DOMDocument.6.0 because my system doesn't have Msxml2.DOMDocument.4.0 installed

And as expected get the following output

>cscript /nologo test50.vbs
Australië

If you are using this in a server environment such as IIS using Classic ASP then there are other steps involved in making sure your output is correctly encoded, but so far you have said you are running this as a standalone script.

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • I have updated the question with the code that I use to read that value. Please take a look and maybe you find something there. Thx – Sebastian Jul 01 '16 at 11:14
  • @Sebastian nothing obvious I can see except your function logic is a bit off. Still waiting for you to reply to [my comment on the question](http://stackoverflow.com/questions/38142080/encoding-of-special-characters-from-xml#comment63716328_38142080). – user692942 Jul 01 '16 at 11:31
  • Sorry, my mistake, we do a stupid thing in this code. We take the xml from the request with request.binaryread. It seems that that conversion from binary read to string, instead of ë returns an à and I can't figure it out why it does that. Thank you anyway for your answer – Sebastian Jul 01 '16 at 13:16
  • @Sebastian Again the problem will be encoding reading UTF-8 data as ASCII during the `BinaryRead()` seen it before. – user692942 Jul 01 '16 at 13:42