ASP Amphasand, XML

J

jbroderick

I have a .asp document on my server it goes as follows
---
<?xml version="1.0" standalone='yes'?>
<Restaurants>
<%
Dim objConn
Dim objRS

set objConn = Server.CreateObject("ADODB.Connection")
set objRS = server.CreateObject("ADODB.recordset")


objconn.open "Driver={SQL
Server};Server=localhost;Uid=user;Pwd=pass;Database=me"
objRS.activeconnection = objConn
objRS.open "SELECT RestaurantName, PremisesAddressLine1 as
Address
FROM tblRestaurantAccounts WHERE franchiseID= 16 and accountstatus=0"


do until objRS.eof
response.write vbtab & "<Restaurant>" & vbcrlf
response.write vbtab & vbtab & "<Shop>" &
objRS.fields("Restaurantname").value & "</Shop>" & vbcrlf
response.write vbtab & vbtab & "<Address>" &
objRS.fields("address").value & "</Address>" & vbcrlf
response.write vbtab & "</Restaurant>" & vbcrlf
objRS.movenext
loop


objRS.close
objConn.close


set objRS = nothing
set objConn = nothing
%>
</Restaurants>
---


the only problem is that it chokes when it gets a record entry that
contains a "&". what code do i need or what modifications to my code to

let this .asp document display a "well-formed" xml document?


thanks for your help


Jonathan Carl Broderick
 
M

Mike Brind

I have a .asp document on my server it goes as follows
---
<?xml version="1.0" standalone='yes'?>
<Restaurants>
<%
Dim objConn
Dim objRS

set objConn = Server.CreateObject("ADODB.Connection")
set objRS = server.CreateObject("ADODB.recordset")


objconn.open "Driver={SQL
Server};Server=localhost;Uid=user;Pwd=pass;Database=me"
objRS.activeconnection = objConn
objRS.open "SELECT RestaurantName, PremisesAddressLine1 as
Address
FROM tblRestaurantAccounts WHERE franchiseID= 16 and accountstatus=0"


do until objRS.eof
response.write vbtab & "<Restaurant>" & vbcrlf
response.write vbtab & vbtab & "<Shop>" &
objRS.fields("Restaurantname").value & "</Shop>" & vbcrlf
response.write vbtab & vbtab & "<Address>" &
objRS.fields("address").value & "</Address>" & vbcrlf
response.write vbtab & "</Restaurant>" & vbcrlf
objRS.movenext
loop


objRS.close
objConn.close


set objRS = nothing
set objConn = nothing
%>
</Restaurants>
---


the only problem is that it chokes when it gets a record entry that
contains a "&". what code do i need or what modifications to my code to

let this .asp document display a "well-formed" xml document?

You need to replace ampersands with &amp;
 
M

Mike Brind

Mike said:
You need to replace ampersands with &amp;

If you are using Google groups, you may not be able to see what I did.
Replace ampersands with the html character entity & amp ; (remove
spaces)
 
R

Rob Meade

...
If you are using Google groups, you may not be able to see what I did.
Replace ampersands with the html character entity & amp ; (remove
spaces)

I seem to remember having problems with XML once before for the same
reason - I believe there are other characters than just the & that cause
problems - I read somewhere before about having to define "ENTITIES" or
something? That's about as far as it went before I just changed my code at
the time to change "&" to "and" - hehe

Rob
 
M

Mike Brind

Rob said:
...


I seem to remember having problems with XML once before for the same
reason - I believe there are other characters than just the & that cause
problems - I read somewhere before about having to define "ENTITIES" or
something? That's about as far as it went before I just changed my code at
the time to change "&" to "and" - hehe

Rob

This might prove useful in future, then:

http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
 
R

Rob Meade

...

Hi Mike,

Thanks for that - I tihnk it was the DTD bit that threw me in the past, my
XML knowledge was/is not the greatest, can do what I need to do but
typically its just config files and stuff as opposed to using anything else
clever to display the data in anyway...

Cheers for the link though - Wiki eh - seems to be taking over the
world....Wiki Wiki Web :eek:)

Rob
 
A

Anthony Jones

I have a .asp document on my server it goes as follows
---
<?xml version="1.0" standalone='yes'?>
<Restaurants>
<%
Dim objConn
Dim objRS

set objConn = Server.CreateObject("ADODB.Connection")
set objRS = server.CreateObject("ADODB.recordset")


objconn.open "Driver={SQL
Server};Server=localhost;Uid=user;Pwd=pass;Database=me"
objRS.activeconnection = objConn
objRS.open "SELECT RestaurantName, PremisesAddressLine1 as
Address
FROM tblRestaurantAccounts WHERE franchiseID= 16 and accountstatus=0"


do until objRS.eof
response.write vbtab & "<Restaurant>" & vbcrlf
response.write vbtab & vbtab & "<Shop>" &
objRS.fields("Restaurantname").value & "</Shop>" & vbcrlf
response.write vbtab & vbtab & "<Address>" &
objRS.fields("address").value & "</Address>" & vbcrlf
response.write vbtab & "</Restaurant>" & vbcrlf
objRS.movenext
loop


objRS.close
objConn.close


set objRS = nothing
set objConn = nothing
%>
</Restaurants>
---


the only problem is that it chokes when it gets a record entry that
contains a "&". what code do i need or what modifications to my code to

let this .asp document display a "well-formed" xml document?


thanks for your help

You are going to run into all sorts of problems with this approach. Not
only are you going to need to at least replace & and < with their
appropriate entities. You would need to add an encoding attribute to the
XML declaration with matches the codepage currently being used by the
response object to encode the strings being written.

You also doing a lot of string contatention to make the output more
readable. Why? Are users going to have to actually read the XML raw?

The Response.ContentType should be set to "text/xml". If you are using
something like XMLHTTP to receive this XML it will be not automatically be
built into a DOM exposed as the responseXML property unless this content
type is set. (and it's good practice to tell the HTTP requester the type of
resource you are sending.)

This is the approach I use:-

<%

Dim oDOM
Dim objConn
Dim objRS
Dim fldName
Dim fldAddress

Set oDOM = Server.CreateObject("MSXML2.DOMDocument.3.0")
set objConn = Server.CreateObject("ADODB.Connection")
set objRS= Server.CreateObject("ADODB.recordset")

oDOM.loadXML "<?xml version=""1.0"" standalone=""yes""
?><Restaurants/>"

objconn.open
"Driver={SQLServer};Server=localhost;Uid=user;Pwd=pass;Database=me"
objRS.activeconnection = objConn
objRS.open "SELECT RestaurantName, PremisesAddressLine1 as Address
FROM tblRestaurantAccounts WHERE franchiseID= 16 and accountstatus=0"

Set fldName = objRS.Fields("RestaurantName")
Set fldAddress = objRS.Fields("PremisesAddressLine1")
Do Until objRS.EOF
With
oDOM.documentElement.appendChild(oDOM.createElement("Restaurant"))
.appendChild(oDOM.createElement("Shop")).text =
fldName.Value
.appendChild(oDOM.createElement("Address")).text =
fldAddress.Value
End With
objRS.movenext
Loop

objRS.close
objConn.close

Response.ContentType = "text/xml"
Response.CharSet = "UTF-8"
oDOM.save Response

%>

It uses a DOM to build the XML which handles all the encoding etc correctly.
The resulting DOM is sent using UTF-8 encoding to the Response.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top