简体   繁体   English

如何将XML文件中的转义unicode字符读入Javascript?

[英]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. 我有一个XML属性,其中包含一个Unicode字符,需要将其读入JavaScript并对其进行操作,而且我很难理解转义的工作方式。 My XML file might contain: 我的XML文件可能包含:

<item foo="\u265c" />

I bring the XML file using XMLHttpRequest, but I have the following result: 我使用XMLHttpRequest带来了XML文件,但结果如下:

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. 我希望y是已解码的Unicode字符。 I could create a function which catches and interprets the \\\\u\u003c/code> string and converts it, but I'm assuming there's a more elegant way to handle it. 我可以创建一个捕获并解释\\\\u\u003c/code>字符串并将其转换的函数,但是我假设有一种更优雅的方式来处理它。

Should I have it stored in the XML file differently, or should I be doing something different on the JavaScript side of things? 我应该以不同的方式将其存储在XML文件中,还是应该在JavaScript方面做一些不同的事情? Thanks for any help anyone can provide. 感谢您提供任何帮助。

Your u265c unicode character in XML or HTML will be expressed like that: &#x265c; 您的XML或HTML u265c unicode字符将表示为: &#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>

The convention \♜ means nothing to XML or to any XML-processing software. 约定\♜对XML或任何XML处理软件没有任何意义。 (The native XML representation would be &#x265c; ). (本机XML表示形式为&#x265c; )。

If you've got a document where, for some reason, the author has decided to represent the character as \♜ rather than &#x265c; 如果您有文档,由于某种原因,作者决定将其表示为\♜而不是&#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. 在XPath 2.0中,编写一个将十六进制转换为十进制的简单函数并不难,然后可以使用codepoints-to-string()函数将十进制数字转换为Unicode字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中解码转义的 unicode javascript 代码? - How do I decode escaped unicode javascript code in Python? 如何使用转义的 unicode 解码字符串? - How do I decode a string with escaped unicode? 如何在javascript中将unicode从HTML输入转换为希腊字符? - How do I convert unicode from an HTML input to a greek character in javascript? 如何在JavaScript中读取XML? - How do I read XML in JavaScript? 如何将包含十六进制 unicode 字符的 innerHTML 更改为另一个与 JavaScript 类似的字符? - How do I change innerHTML which contains hex unicode character for another similar character with JavaScript? 如何使用JQuery的parseXML将转义的字符串解析为XML对象? - How do I use JQuery's parseXML to parse from escaped string to an XML Object? 如何使用javaScript从url读取txt文件? - How do I read txt file from url with javaScript? 如何让 onClick() 从我的 javascript 文件中读取? - How do I let onClick() read from my javascript file? 使用转义的unicode解码字符串(5个字符) - Decode string with escaped unicode (5 character) 从 Node.JS 中的文本文件读取转义的 unicode 序列不会在控制台或 api 响应中呈现 - Reading escaped unicode sequence from a text file in Node.JS do not render on console or api response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM