简体   繁体   English

从XML文件解析的数据表示“%22%”,表示“%27%”……如何解决此问题?

[英]Data parsed from an XML file renders %22% for " and %27% for '…how do I fix this?

I am parsing data from an XML file and the XML has the character ' encoded as %27% and the character " as %22%. Those characters render in my HTML, so I end up with data like this: 我正在从XML文件中解析数据,XML的字符'编码为%27%,字符“编码为%22%。这些字符在我的HTML中呈现,因此最终得到如下数据:

Pike%27%s Peak 派克%27%s峰值

or 要么

this is my %22%Quote%22% 这是我的%22%Quote%22%

I attempted to perform a string.replace(), but to no avail. 我尝试执行string.replace(),但无济于事。 I am such a newbie when it comes to parsing XML with javascript. 当我用JavaScript解析XML时,我真是个新手。 I'm sure my code is way off, but that's why I'm posting this quest...can anyone help me out? 我确定我的代码还差得很远,但这就是为什么我发布此任务的原因...有人可以帮我吗? Here's the code for my attempted string.replace(): 这是我尝试的string.replace()的代码:

function xmlencode(string) {
return string.replace(/'%27'/g,'\'').replace(/'%22'/g,'\"');

} }

Your feedback is much appreciated. 非常感谢您的反馈。

Thanks, Carlos 谢谢,卡洛斯

That's some proprietary encoding, that's nothing that is usually used in an XML file. 那是一些专有的编码,在XML文件中通常没有使用。 Because of that, there is no standard way of decoding it. 因此,没有标准的解码方法。

You have some extra apostrophes in your function that keeps it from working: 您的函数中有些多余的撇号使它无法正常工作:

function decode(string) {
  return string.replace(/%27%/g,"'").replace(/%22%/g,'"');
}

You can parse the hex code between the percent signs to get a function that works for any such entity: 您可以解析百分号之间的十六进制代码,以获得适用于任何此类实体的函数:

function decode(string) {
  return string.replace(/%[\da-f]{2}%/gi,function(m) {
    return String.fromCharCode(parseInt(m.substr(1,2), 16));
  });
}

Example: 例:

alert(decode("%49%%74%%20%%77%%6f%%72%%6b%%73%%21%"));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM