ASP Amphasand, XML

Discussion in 'ASP General' started by jbroderick@gmail.com, Jul 19, 2006.

  1. Guest

    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
    , Jul 19, 2006
    #1
    1. Advertising

  2. Mike Brind Guest

    wrote:
    > 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;

    --
    Mike Brind
    Mike Brind, Jul 19, 2006
    #2
    1. Advertising

  3. Mike Brind Guest

    Mike Brind wrote:
    > wrote:
    > > 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;
    >
    > --
    > Mike Brind


    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)
    Mike Brind, Jul 20, 2006
    #3
  4. Rob Meade Guest

    "Mike Brind" wrote ...

    > 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
    Rob Meade, Jul 20, 2006
    #4
  5. Mike Brind Guest

    Rob Meade wrote:
    > "Mike Brind" wrote ...
    >
    > > 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


    This might prove useful in future, then:

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

    --
    Mike Brind
    Mike Brind, Jul 20, 2006
    #5
  6. Rob Meade Guest

    "Mike Brind" wrote ...

    > This might prove useful in future, then:
    > http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references


    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
    Rob Meade, Jul 20, 2006
    #6
  7. <> wrote in message
    news:...
    > 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.

    >
    > Jonathan Carl Broderick
    >
    Anthony Jones, Jul 21, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bomb Diggy
    Replies:
    0
    Views:
    435
    Bomb Diggy
    Jul 28, 2004
  2. Tony Prichard
    Replies:
    0
    Views:
    718
    Tony Prichard
    Dec 12, 2003
  3. XML, asp amphasand

    , Jul 19, 2006, in forum: XML
    Replies:
    4
    Views:
    1,363
  4. Matt
    Replies:
    3
    Views:
    331
    Bob Barrows [MVP]
    Apr 23, 2004
  5. Erik Wasser
    Replies:
    5
    Views:
    438
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page