How to generate pure XML page with ASP?

V

vunet.us

How to generate pure XML page with ASP?
This try gave me an error:

XML Parsing Error: not well-formed
Location: http://www.worldincatalog.com/manage/edit_item_xml.asp?itemid=15
Line Number 2, Column 26: <font face="Arial" size=2>
-------------------------^

<% @ Language="VBScript" %>
<%
Option Explicit
Response.Buffer = True
Response.ContentType = "text/xml"

' Start XML document.
Response.Write "<?xml version=""1.0""?>" & vbCrLf
%>
<!-- #Include file="adocon.inc" -->
<%
Set rsItem = Server.CreateObject("ADODB.Recordset")
ItemSQL = "...xxx..."
rsItem.Open ItemSQL, adoCon
if not rsItem.eof then

Response.Write "<ItemXML>" & vbCrLf
Response.Write "<ItemID>"&rsItem("ItemID")&"</ItemID>" &
vbCrLf
Response.Write "</ItemXML>" & vbCrLf

end if
rsItem.close
set rsItem = nothing
set ItemSQL = nothing
set adoCon = nothing
%>
 
D

Daniel Crichton

How to generate pure XML page with ASP?
This try gave me an error:

XML Parsing Error: not well-formed
Location: http://www.worldincatalog.com/manage/edit_item_xml.asp?itemid=15
Line Number 2, Column 26: <font face="Arial" size=2>
-------------------------^

This tends to mean there's an error in the code, and the ASP debugger is
spitting out where the problem is, but your browser is trying to parse it as
XML. View the source to see the error message, or comment out the line
generating the xml header.

Dan
 
A

Anthony Jones

How to generate pure XML page with ASP?
This try gave me an error:

XML Parsing Error: not well-formed
Location: http://www.worldincatalog.com/manage/edit_item_xml.asp?itemid=15
Line Number 2, Column 26: <font face="Arial" size=2>
-------------------------^

<% @ Language="VBScript" %>
<%
Option Explicit
Response.Buffer = True
Response.ContentType = "text/xml"

' Start XML document.
Response.Write "<?xml version=""1.0""?>" & vbCrLf
%>
<!-- #Include file="adocon.inc" -->
<%
Set rsItem = Server.CreateObject("ADODB.Recordset")
ItemSQL = "...xxx..."
rsItem.Open ItemSQL, adoCon
if not rsItem.eof then

Response.Write "<ItemXML>" & vbCrLf
Response.Write "<ItemID>"&rsItem("ItemID")&"</ItemID>" &
vbCrLf
Response.Write "</ItemXML>" & vbCrLf

end if
rsItem.close
set rsItem = nothing
set ItemSQL = nothing
set adoCon = nothing
%>


Build the XML using a DOM. There so many pitfuls to writing XML directly to
Response it just isn't worth it in most cases:-

<%

Dim oDOM : Set oDOM = Server.CreateObject("MSXML2.DOMDocument.3.0")
oDOM.LoadXML "<ItemXML />"

'Recordset stuff here

AddElem oDOM.documentElement, "ItemID", rsItem("ItemID")

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


Function AddElem(roParent, rsName, rvntValue)
Set AddElem = roParent.ownerDocument.createElement(rsName)
roParent.appendChild AddElem
If Not IsNull(rvntValue) AddElem.Text = rvntValue
End Function

%>
 
V

vunet.us

Build the XML using a DOM. There so many pitfuls to writing XML directly to
Response it just isn't worth it in most cases:-

<%

Dim oDOM : Set oDOM = Server.CreateObject("MSXML2.DOMDocument.3.0")
oDOM.LoadXML "<ItemXML />"

'Recordset stuff here

AddElem oDOM.documentElement, "ItemID", rsItem("ItemID")

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

Function AddElem(roParent, rsName, rvntValue)
Set AddElem = roParent.ownerDocument.createElement(rsName)
roParent.appendChild AddElem
If Not IsNull(rvntValue) AddElem.Text = rvntValue
End Function

%>

this generates the output:

<ItemXML/>

What's wrong?

<% @ Language="VBScript" %>
<%
Function AddElem(roParent, rsName, rvntValue)
Set AddElem = roParent.ownerDocument.createElement(rsName)
roParent.appendChild AddElem
If Not IsNull(rvntValue) AddElem.Text = rvntValue
End Function

Dim oDOM : Set oDOM = Server.CreateObject("MSXML2.DOMDocument.
3.0")
oDOM.LoadXML "<ItemXML />"

'included adoCon
Set rsItem = Server.CreateObject("ADODB.Recordset")
ItemSQL = "SELECT * FROM TABLE WHERE ItemID = '" & request("id") &
"';"
rsItem.Open ItemSQL, adoCon
if not rsItem.eof then

AddElem oDOM.documentElement, "ItemID", rsItem("ItemID")
AddElem oDOM.documentElement, "Title", rsItem("Title")

end if
rsItem.close
set rsItem = nothing
set ItemSQL = nothing

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

Anthony Jones

this generates the output:

<ItemXML/>

What's wrong?

<% @ Language="VBScript" %>
<%
Function AddElem(roParent, rsName, rvntValue)
Set AddElem = roParent.ownerDocument.createElement(rsName)
roParent.appendChild AddElem
If Not IsNull(rvntValue) AddElem.Text = rvntValue
End Function

Dim oDOM : Set oDOM = Server.CreateObject("MSXML2.DOMDocument.
3.0")
oDOM.LoadXML "<ItemXML />"

'included adoCon
Set rsItem = Server.CreateObject("ADODB.Recordset")
ItemSQL = "SELECT * FROM TABLE WHERE ItemID = '" & request("id") &
"';"
rsItem.Open ItemSQL, adoCon
if not rsItem.eof then

AddElem oDOM.documentElement, "ItemID", rsItem("ItemID")
AddElem oDOM.documentElement, "Title", rsItem("Title")

Try adding this code here:-

Else

AddElem oDOM.documentElement "Fail", "No Record for: " &
Request("id")

end if
rsItem.close
set rsItem = nothing
set ItemSQL = nothing

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

I suspect the query isn't returning what you expect.

Also be explicit in using the Request object :- Request.QueryString("id") is
better.

I hope you will use a command object in production code. Concatenting data
from the client into a string that is executed as SQL leaves you open to SQL
injection attacks.

Use SELECT ItemID, Title instead of *.
 
V

vunet.us

Try adding this code here:-

Else

AddElem oDOM.documentElement "Fail", "No Record for: " &
Request("id")



I suspect the query isn't returning what you expect.

Also be explicit in using the Request object :- Request.QueryString("id") is
better.

I hope you will use a command object in production code. Concatenting data
from the client into a string that is executed as SQL leaves you open to SQL
injection attacks.

Use SELECT ItemID, Title instead of *.

i am embarrased to say but this error happens when item does not
exist, otherwise the code works well. thank you for the hint. you are
great.
 

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

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top