Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为特殊字符编写正确的xml_Javascript_Xml_Svg - Fatal编程技术网

Javascript 如何为特殊字符编写正确的xml

Javascript 如何为特殊字符编写正确的xml,javascript,xml,svg,Javascript,Xml,Svg,我正在尝试从使用特殊字符的字符串编写正确的svg。我正在为svg文档使用UTF-8编码。我正在使用javascript。 字符串类似于: <glyph unicode="&#xa5;" horiz-adv-x="819" /> 在使用DOMParser api编写xml时,它会将¥转换为不正确的xml,浏览器会抛出xml解析错误 下面是我想转换为SVG文档的完整文本 <?xml version="1.0" standalone="no"?> <!DOC

我正在尝试从使用特殊字符的字符串编写正确的svg。我正在为svg文档使用UTF-8编码。我正在使用javascript。 字符串类似于:

<glyph unicode="&#xa5;" horiz-adv-x="819" />

在使用DOMParser api编写xml时,它会将¥转换为不正确的xml,浏览器会抛出xml解析错误

下面是我想转换为SVG文档的完整文本

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"   "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
<svg xmlns="http://www.w3.org/2000/svg">
<metadata></metadata>
  <defs>
    <font id="DearestRegular" horiz-adv-x="2048" >
      <font-face units-per-em="2048" ascent="1638" descent="-410" />
      <glyph unicode="&#xa5;" horiz-adv-x="819" />
    </font>
 </defs>
</svg>

使用DOMParser,我得到如下输出:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"   "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
<svg xmlns="http://www.w3.org/2000/svg">
<metadata></metadata>
  <defs>
    <font id="DearestRegular" horiz-adv-x="2048" >
      <font-face units-per-em="2048" ascent="1638" descent="-410" />
      <glyph unicode="¥" horiz-adv-x="819" />
    </font>
 </defs>
</svg>


这会在浏览器中引发XML解析错误。

这不是不正确的XML

可能您的服务器在HTTP头中发送了错误的消息


(因为XML声明没有另外说明,所以应该使用UTF-8)

我只是在本地使用ajax和XMLHttpRequest对象来读取svg文件。下面是代码:var req=newXMLHttpRequest();请求打开(“GET”,“Fonts/myFont.svg”,true);req.onreadystatechange=function(){if(4==req.readyState){var text=req.responseText,dom=(new DOMParser()).parseFromString(text,“image/svg+xml”)};请求发送(空);我得到的doc对象给出了字符¥的XML解析错误。它应该是¥;获取正确的svg文档。