hidura
hidura

Reputation: 693

How to specify the encoding of an XML document using Javascript

Hello i am trying to create a document using Javascript and there's a problem with the encoding of the document, because reject all the non-ascii characters, the string that im passing is this "verificación" but is replaced with this "�", how i can fixed that.

This is my code:

function createDoc(string){
    if (window.DOMParser)
      {
        parser = new DOMParser();


        doc = parser.parseFromString('<?xml version="1.0" encoding="UTF-8"?>'+string, "text/xml");
      }
    else // Internet Explorer
      {
        doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = "false";
        doc.loadXML('<?xml version="1.0" encoding="UTF-8"?>'+string);
      }

    return doc
}

Thanks in advance.

Upvotes: 1

Views: 3214

Answers (1)

Martijn
Martijn

Reputation: 13632

Javascript strings are all UTF-16-encoded. You could try specifying that.

Where does the string come from? Is the string correct before parsing it?

Also, when is it being displayed? What encoding is expected there?

Upvotes: 3

Related Questions