如何解析带有特殊字符的xml

如何解析带有特殊字符的xml

本文介绍了如何解析带有特殊字符的xml?专门用于&符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从文本框中获取数据并将其更改为 xml 格式并将其存储在数据库中.为了允许特殊字符,我编写了 javascript 函数来用它的 html 实体替换特殊字符.

I am getting data from text box and change it into xml format and store it in data base. For allowing special characters i wrote javascript function to replace special character with its html entities.

 "     "
 &     &
 <     &lt;
 >     &gt;

对于引号,小于,大于"它的工作正常.对于&"它显示 xml 解析器错误我用javascript用它的实体替换特殊字符

for "quotes , less than , greater than" its working fine. for "&" it is showing xml parser error i used javascript to replace special character with its entity

  string.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, "\\'");

  for "&" allow showing warning but it get stored in data base. please help me to sort out this problem .


 i begin with string.replace(/&/g, '&amp;') even though i am getting

警告:SimpleXMLElement::__construct():实体:第 9 行:解析器错误:EntityRef:期望 ';'在/var/www/我也试过这个 &amp;amp;如此链接中所述stackoverflow.com/questions/1328538/...
之后没有警告,但在保存在数据库中时,它保存为ab &amp cd"

Warning: SimpleXMLElement::__construct(): Entity: line 9: parser error : EntityRef: expecting ';' in /var/www/ i tried this also &amp;amp; as mentioned in this link stackoverflow.com/questions/1328538/…
After that there is no warning but while saving in db it saved as "ab &amp cd"

推荐答案

Start 替换 & 字符,then 替换另一个人物.否则,您将用 &amp;

Start with replacing the & character, then replace the other characters. Otherwise you will replace & from the previous entities (&lt; etc.) by &amp;

string.replace(/&/g, '&amp;amp;') //<= start with
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;')
      .replace(/"/g, '&quot;')
      .replace(/'/g, '&apos');
// &apos; may be "\\'",  depends on how te OP wants to use it

[edit based on comments]使用&amp;amp;替换&符号

[edit based on comments] use &amp;amp; to replace the ampersand character

这篇关于如何解析带有特殊字符的xml?专门用于&符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:00