Google Answers Logo
View Question
 
Q: XML and & Symbol in ASP ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: XML and & Symbol in ASP
Category: Computers > Programming
Asked by: xemion-ga
List Price: $18.00
Posted: 16 Jan 2003 12:02 PST
Expires: 15 Feb 2003 12:02 PST
Question ID: 144328
I'm having problems with an XML feed.  I use the below ASP code to
download the feed:

Set objSrvHTTP = Server.CreateObject ("MSXML2.ServerXMLHTTP")
objSrvHTTP.open "GET","http://www.website.com?getfeed=1", false
objSrvHTTP.send

if objSrvHTTP.responseXML.getElementsByTagName("ErrCount").item(0).text
<> 0 then

the feed looks like this:

<?xml version="1.0"?>
<interface-response>
<host>
  <name>@</name>
  <type>FRAME</type>
  <address>http://www.website.com?x=1&y=2</address>
</host>
<ErrCount>0</ErrCount>
<debug><![CDATA[]]></debug>
</interface-response>

The problem is with the <address> data.  When that data is a full url
with parameters (with a "&y=2") I get an error.  The error looks like
this:

Microsoft VBScript runtime (0x800A01A8)
Object required: '[object]'

The line at which the error occurs is the "if error = 0 then" line.

If the <address> field does not have a "&" and looks just like
"http://www.website.com?x=1" then I have no problems.

My question is...why is that "&" causing an error when I try to parse
my XML?  Also, if you can answer how to keep it from happening, that
would be greatly appreciated (think "tip" ;-).

Please ask any questions if you have them, thank you.  As always,
rating will be give.

xemion-ga

Request for Question Clarification by sgtcory-ga on 16 Jan 2003 13:18 PST
Hello xemion,

Is this XML feed data yours, or is it third party? I'm wondering if
you have the ability to change the originating data?

Thanks,
SgtCory
Answer  
Subject: Re: XML and & Symbol in ASP
Answered By: davebug-ga on 16 Jan 2003 14:05 PST
Rated:5 out of 5 stars
 
As the ampersand is used to signify the encoding of an entity (ie
escaped character) it needs to be, itself, encoded as an entity. Thus,
the url address will need to be entered as:

http://www.website.com?x=1&amp;y=2

wherein the ampersand in the URL is replaced with &amp;

In order to ensure that this encoding comes across your way accurately
and is not interpreted by the browsers, I'm going to post this with
spaces between each character:
& a m p ;

There are a total of five predefined entities available in XML,
further explained and listed here:
http://msxml.com/intro_xml/entities.html
---
In some articles on the subject it is recommended, for
interoperability with SGML and other mark-up languages, that the
entity name be replaced with &#38;#38;

XML Entities and their Applications
(in the subsection of "Predefined Entities")
http://tech.irt.org/articles/js212/#character_references

Let me know if this fully answers your question and feel free to ask
for any further clarifications.

Thanks for using Google Answers,
Dave Bug

Request for Answer Clarification by xemion-ga on 16 Jan 2003 21:04 PST
Ah, ok.  I did not realize that effect XML.  Out of curiousity
now...let's say I can't change the data coming into the ServerXMLHTTP
object...can I do any sort of Replace() command or anything to replace
the "&" with "&amp;"?  Do you have any recommendations on how to fix
the problem without modifying the data before I get it?  Thanks.

xemion-ga

Clarification of Answer by davebug-ga on 17 Jan 2003 09:22 PST
Without knowing the full application, code, etc. I can't promise you
that this will work within your code, but yes, a replace function
should work with the following:

replace(str,"&","&amps;")
xemion-ga rated this answer:5 out of 5 stars
Thanks for the help!

Comments  
Subject: Re: XML and & Symbol in ASP
From: richardli-ga on 16 Jan 2003 13:37 PST
 
Attributes and any other parsed character text that use the reserved
characters &, > or < have to encode them as character entities. 
Replace the ampersand with
&amp;

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy