You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by tom john <cy...@yahoo.com> on 2002/05/16 17:00:54 UTC

Parsing special characters like '&'

I need to create xml document from string. if the text
contains special characters like '&' it throws error.
is there any solution for this... 
may be some special feature that i can set for the
parser.


__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Parsing special characters like '&'

Posted by Bob Jamison <rj...@lincom-asg.com>.
tom john wrote:

>I need to create xml document from string. if the text
>contains special characters like '&' it throws error.
>is there any solution for this... 
>may be some special feature that i can set for the
>parser.
>
>
>__________________________________________________
>Do You Yahoo!?
>LAUNCH - Your Yahoo! Music Experience
>http://launch.yahoo.com
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
>For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>
>
>  
>
Tom,

A little method like this can maybe fix your
problem.  It processes a raw string into an
XML-compatible one:


String xmlString(String val)
{
  if (val==null)
    return val;
  StringBuffer buf = new StringBuffer();
  for (int i=0;i<val.length();i++)
      {
      char ch = val.charAt(i);
      if (ch=='&')
        buf.append("&amp;");
      else if (ch=='<')
        buf.append("&lt;");
      else if (ch=='>')
        buf.append("&gt;");
      else if (ch=='\'')
        buf.append("&apos;");
      else if (ch=='"')
        buf.append("&quot;");
      else
        buf.append(ch);
      }
  val=buf.toString();
  return val;
}



It will turn something like

"Rock & roll"

into

&quot;Rock &amp; roll&quot;






Hope this helps.


Bob Jamison
LinCom Corp



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org