Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't create XML node with cyrillic name in IE11

I need to create a xml document (with JavaScript) containing nodes, which is named in russian.

I get InvalidCharacterError in IE11 when trying run doc.createElement("Выборка")

doc is created with var doc = document.implementation.createDocument("", "", null)

In other browsers this code is working without any issues.

How can be solved? What is the root of an issue?

jsFiddle example: http://jsfiddle.net/e4tUH/1/

My post on connect.microsoft.com: https://connect.microsoft.com/IE/feedback/details/812130/cant-create-xml-node-with-cyrillic-name-in-ie11

Current workaround: Switch IE11 to IE10 with X-UA-Compatible meta-tag and use window.ActiveXObject(...) to create XML documents.

like image 858
Olegas Avatar asked Dec 18 '13 14:12

Olegas


1 Answers

Maybe IE11 has an issue similar to what Firefox had in the past:

https://bugzilla.mozilla.org/show_bug.cgi?id=431701

That means that although your page is loading the correct encoding, IE11 is creating the new document with a default encoding which is not the expected one. There's no way to check that besides looking into IE11 source code, which we don't have.

Have you trying to add non-ASCII characters in other places besides element names? Like an attribute value or a text node?

I searched how to change the created document encoding and haven't found any solution for that.

To solve your problem I would suggest to use a DOMParser and generate a document from a XML string, like the following:

var parser=new DOMParser();
var xmlDoc=parser.parseFromString('<?xml version="1.0" encoding="UTF-8"?><Выборка>Выборка текста</Выборка>',"text/xml");

All browsers seems to support it for XML parsing. More about DOMParser on the following links, including how to provide backward compatibility with older IE versions:

http://www.w3schools.com/dom/dom_parser.asp

https://developer.mozilla.org/en-US/docs/Web/API/DOMParser

If you don't want to generate your XML just by concatenating strings, you can use some kind of XML builder like in this example: http://jsfiddle.net/UGYWx/6/

Then you can easily create your XML in a more safe manner:

var builder = new XMLBuilder("rootElement");
builder.text('Some text');

var element = builder.element("someElement", {'attr':'value'});
element.text("This is a text.");

builder.text('Some more Text');
builder.element("emptyElement");
builder.text('Even some more text');

builder.element("emptyWithAttributes", {'a1': 'val1', 'a2' : 'val2'});

$('div').text(builder.toString());
like image 100
visola Avatar answered Oct 20 '22 12:10

visola