• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

Need help with XML encoding

I'm having trouble with encoding my XML document. What I'm trying to do is take data from an MS Access database and make an RSS feed using ASP.
The hitch I keep running into is dealing with fields in my database that have ampersands and quotes and that kind of thing. I've tried 3 different types of encoding the Doctype but each produces some kind of error.

I'm new to XML and creating RSS feeds so I'm unsure of the Encoding I should use and how to handle & and " and special characters.
The code I'm using, I found on a website and have adapted for my use. It worked fine and Validated perfectly unmodified. But once I used it on my database with the special characters, I kept getting errors.
This is towards the top:
<?xml version="1.0" encoding="UTF-8"?>   (note, i've tried iso-8859-1 and also a MS one too)
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns="http://purl.org/rss/1.0/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
 
 
and then later when I'm getting my data:
'// Now loop through each item, and output all its data --------------------------------------
For i = 0 To UBound(arrItems, 2)
'// Begin item (include the url)
	Response.Write _
	"<item>" & vbCrLf
'// Add title
	Response.Write _
	"<title>" & arrItems(1, i) & "</title>" & vbCrLf
'// Add description tag
	Response.Write _
	"<description><![CDATA[" & arrItems(5, i) & "]]></description>" & vbCrLf
'// Add the contents tag
'// This can be same as the description, but may differ as well
	Response.Write _
	"<content:encoded><![CDATA[" & arrItems(5, i) & "]]></content:encoded>" & vbCrLf
	Response.Write _
	"<link>" & arrItems(6, i) & "</link>" & vbCrLf
'// Add the poster/author
	Response.Write _
	"<dc:creator>" & arrItems(5, i) & "</dc:creator>" & vbCrLf
'// And finally, close the item
	Response.Write _
	"</item>" & vbCrLf & vbCrLf
Next
and so on....

Open in new window

0
sasanders
Asked:
sasanders
  • 4
  • 3
2 Solutions
 
MorcalavinCommented:
Have you tried using the Server.HTMLEncode()  method on the strings you are placing in your xml?

<%
dim foo
foo = """M & M's are yummy!"", said some random guy."
Response.Write Server.HTMLEncode(foo) 'data is changed to &quot;M &amp; M's are yummy!&quot;, said some random guy.
%>

Open in new window

0
 
sasandersAuthor Commented:
Nope, haven't tried that yet. I'm trying a few other things and may end up trying your suggestion though. thanks.
0
 
b0lsc0ttCommented:
sasanders,
Are you asking or need some other type of encoding?  You mentioned needing to handle characters like the double quote (") and the ampersand (&).  Do you know what you need to use for them or is this some type of custom encoding?
The expert's recommendation above will easily change those characters and other reserved html characters to their accepted entities so your code will be valid.  Please confirm or clarify what you need since your response of "may end up trying" seems odd since that would seem to be just the solution you asked for.
bol
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sasandersAuthor Commented:
Ok, will do. I was just troubleshooting my rss feed - without the encoding issue. Once I'm done with that, I can get back to handling the special characters.
0
 
sasandersAuthor Commented:
Not sure how to adapt code for my link:

Response.Write _
"<link>" & arrItems(6, i) & "pressdetail.asp?PrID=" & arrItems(0, i) & "</link>" & vbCrLf

to Response.Write Server.HTMLEncode

can you help?
0
 
b0lsc0ttCommented:
To use it in your line you would do ...
Response.Write _
"<link>" & Server.HTMLEncode(arrItems(6, i)) & "pressdetail.asp?PrID=" & Server.HTMLEncode(arrItems(0, i)) & "</link>" & vbCrLf
Of course if you knew it was just one part that needed the encoding then it is easier to use the method just for the one variable.
bol
0
 
sasandersAuthor Commented:
Thanks for all your help. I do have my feed validating. So I could stop now.
But I want to add more tags (i simplified it in order to troubleshoot). And I'm positive my code is inefficient, so I may end up trying to perfect it before offering it to the public.
0
 
b0lsc0ttCommented:
sasanders,
I'm glad it is validating now.  Good idea to improve the feed before making it public but you should probably open a new question for that if it is different from what you asked here.  Feel free to post the URL of the new question here if you want our attention to it.  Let us know if you need anymore help with what you asked here or have a question about closing this (if it is done).
bol
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now