TobyRush
TobyRush

Reputation: 756

How do I read an escaped unicode character from an XML file into Javascript?

I have an XML attribute which contains a Unicode character which I need to read into JavaScript and act upon, and I'm having a hard time understanding how escaping works. My XML file might contain:

<item foo="\u265c" />

I bring the XML file using XMLHttpRequest, but I have the following result:

x = itemObject.getAttribute('foo') // x = "\\u265c"
y = decodeURIComponent(x)          // y = "\\u265c"

What am I missing here? I want y to be the decoded Unicode character. I could create a function which catches and interprets the \\u string and converts it, but I'm assuming there's a more elegant way to handle it.

Should I have it stored in the XML file differently, or should I be doing something different on the JavaScript side of things? Thanks for any help anyone can provide.

Upvotes: 1

Views: 774

Answers (2)

Michael Kay
Michael Kay

Reputation: 163504

The convention \u265c means nothing to XML or to any XML-processing software. (The native XML representation would be &#x265c;).

If you've got a document where, for some reason, the author has decided to represent the character as \u265c rather than &#x265c;, then you'll have to find some way of decoding it at application level. In XPath 2.0 it's not too difficult to write a simple function that converts hex to decimal, and then you can use the codepoints-to-string() function to convert the decimal number to a Unicode character.

Upvotes: 1

Jonathan Applebaum
Jonathan Applebaum

Reputation: 5986

Your u265c unicode character in XML or HTML will be expressed like that: &#x265c; .
see it working here.

You can also write a conversion function:
working example

<!DOCTYPE html>
<html>
  <head>
    <style>
    </style>
  </head>
  <body>
    <input type="button" value="convert" onClick="convert('u265c')"/>
    <span id="myspan"></span>


    <script>
    function convert(unchar)
      {
        var base = '&#x';
        var fixed = unchar.replace("u","");
        document.getElementById("myspan").innerHTML = base + fixed + ";";
      }

    </script>
  </body>
</html>

Upvotes: 1

Related Questions