admin管理员组

文章数量:1336321

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;

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 

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/questions/1328538/…
After that there is no warning but while saving in db it saved as "ab &amp cd"

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.

 "     &quot;
 &     &amp;
 <     &lt;
 >     &gt;

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 

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./questions/1328538/…
After that there is no warning but while saving in db it saved as "ab &amp cd"

Share Improve this question edited May 23, 2017 at 11:43 CommunityBot 11 silver badge asked Jul 19, 2012 at 7:29 suganyasuganya 2452 gold badges4 silver badges12 bronze badges 2
  • 1 Once you write parse error, once warning - what's the case now? Can you post the error/warning-message please? – Christoph Commented Jul 19, 2012 at 7:37
  • What database? Where is the JS running (browser? node? soething else?)? Is the JS talking directly to the database or do you have some server side program in between? – Quentin Commented Jul 19, 2012 at 8:09
Add a ment  | 

1 Answer 1

Reset to default 6

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 ments] use &amp;amp; to replace the ampersand character

本文标签: javascripthow to parse xml with special character specifically for ampersandStack Overflow