Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

special characters in database

Status
Not open for further replies.

nam4520

Programmer
Jan 7, 2003
14
US
I have special characters such as &,<,>,&quot; already in the data i'm trying to read from my database. I am using ASP with xml. I can use a &quot;Replace function&quot; to replace some of these characters with the replacement entities such as &quot;&lt;&quot; as I loop through to output the fields. But I have no clue what to do about the quote. Is there a way in XML of hiding these characters if they are already in the database.
 
The XML page cannot be displayed

Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

End tag 'note_body' does not match the start tag 'PF8'. Error processing resource ' Line 107, Position 46

USE <PF7> AND <PF8> TO NAVIGATE REPORT LIST</note_body><note_dt>8/28/99 11:30:11 AM</note_dt><note_user>DREYNOLDS</note_user><note_body>FMID(s) SWJR371, SWJR381, SWJR382, SWJR410 are not being addressed by this Change because:
---------------------------------------------^


above is the error message that i get when i run my asp page. the <PF7> and others are data in the database.
below is my code. the Xml processor interpretes that as a tag.


<% @language=&quot;vbscript&quot; %>
<% Response.ContentType=&quot;text/xml&quot; %>
<?xml version=&quot;1.0&quot;?>
<!-- xml file generated by asp -->

<notes xml:space=&quot;preserve&quot;>


dim omsg, rst
set omsg= Server.CreateObject(&quot;ADODB.Connection&quot;)
omsg.ConnectionString=&quot;Provider=MSDASQL;DSN=SwisODBC;UID=SYSDBA;PWD=asdf&quot;
omsg.open

set rst= Server.CreateObject(&quot;ADODB.Recordset&quot;)
set rst=omsg.Execute(&quot; Select * from CHGNOTE;&quot;)
do until rst.EOF
Response.Write &quot;<note_dt>&quot; & (replace(rst(&quot;NOTE_DT&quot;), &quot;&&quot;, &quot;&amp;&quot;)) & &quot;</note_dt>&quot;
Response.Write &quot;<note_user>&quot; & (replace(rst(&quot;NOTE_USER&quot;), &quot;&&quot;, &quot;&amp;&quot;)) & &quot;</note_user>&quot;
Response.Write &quot;<note_body>&quot; & (replace(rst(&quot;NOTES&quot;), &quot;&&quot;, &quot;&amp;&quot;)) & &quot;</note_body>&quot;
rst.MoveNext
loop
rst.Close
set rst=nothing
%>
</notes>
 
Response.Write &quot;<note_dt>&quot; & (replace(rst(&quot;NOTE_DT&quot;), &quot;&&quot;, &quot;&&quot;)) & &quot;</note_dt>&quot;
Response.Write &quot;<note_user>&quot; & (replace(rst(&quot;NOTE_USER&quot;), &quot;&&quot;, &quot;&&quot;)) & &quot;</note_user>&quot;
Response.Write &quot;<note_body>&quot; & (replace(rst(&quot;NOTES&quot;), &quot;&&quot;, &quot;&&quot;)) & &quot;</note_body>&quot;

The problem you're seeing is because you're doing string concatenation to build your XML, and the special characters (Entity Reference, in XML-speak) aren't being escaped. The absolutely correct way to do this is instantiate a DOM document object, and add your nodes to it programmatically. Assigning a value to a NodeElement's Text property does the escaping for you.

If you really want to continue doing it the way you are, you can write a function to replace the ampersand, left angle-bracket, right angle-bracket, and the single & double quote marks with their escaped equivalents (& amp; & lt; & gt; & apos; & quot;) (get rid of the excess spaces), but it's a band-aid solution.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top