XML/ASP Help Needed

L

lreames

I am new to XML, but not ASP.


I have the following XML that I load via


Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.loadXML xmldom.xml


<deals><escanoffers><escanoffer offerid="3743" itemnumber="502022"
upc="4300094509" dealamt="0.20" begindate="03/01/2006"
enddate="03/30/2006" returnby="02/28/2006" selectionlength="14"
maxretail="0.00" /></escanoffers></deals>


I need to parse it, getting all the attributes out, using ASP.


AHIA
Larry...
 
B

Bob Barrows [MVP]

I am new to XML, but not ASP.


I have the following XML that I load via


Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.loadXML xmldom.xml


<deals><escanoffers><escanoffer offerid="3743" itemnumber="502022"
upc="4300094509" dealamt="0.20" begindate="03/01/2006"
enddate="03/30/2006" returnby="02/28/2006" selectionlength="14"
maxretail="0.00" /></escanoffers></deals>


I need to parse it, getting all the attributes out, using ASP.

You mean "using vbscript" don't you? or are you using jscript in your asp
page?

Let's assume you want to put the attribute values into a Dictionary object:

dim d, node, attrs, attr, i
set d = createobject("scripting.dictionary")
set node = nothing
set node = xml.selectsinglenode("/deals/escanoffers/escanoffer")
if not node is nothing then
set attrs = node.attributes
for i = attrs.length-1
set attr=attrs.item(i)
d.add attr.nodename, attr.nodevalue
next
end if

HTH,
Bob Barrows
 
L

lreames

Bob,

Thanks for the response.

Should the FOR line read....
for i = 0 to attrs.length-1

The script is not making it past
if not node is nothing then

I added the last two lines
set attr=attrs.item(i)
response.write "nodename:" & attr.nodename
response.write "nodevalue:" & attr.nodevalue

I need to display the data in the XML doc, is this syntax correct?

Also, there are several escanoffer within escanoffers.

TIA,
Larry...
 
B

Bob Barrows [MVP]

Bob,

Thanks for the response.

Should the FOR line read....
for i = 0 to attrs.length-1
Yes

The script is not making it past
if not node is nothing then
That means your actual xml is different from the example xml you showed me.
The selectsinglenode method is not finding that node.
I added the last two lines
set attr=attrs.item(i)
response.write "nodename:" & attr.nodename
response.write "nodevalue:" & attr.nodevalue

I need to display the data in the XML doc, is this syntax correct?

Seems to be. You probably want to add some said:
Also, there are several escanoffer within escanoffers.
Then you need to modify the example to:
dim nodes, node, attrs, attr, i
set nodes = nothing
set node = xml.selectnodes("/deals/escanoffers")
if not nodes is nothing then
for i = 0 to nodes.length - 1
set node = nodes.nextnode
set attrs = node.attributes
for i = attrs.length-1
set attr=attrs.item(i)
response.write "nodename:" & attr.nodename
response.write "nodevalue:" & attr.nodevalue
next
next
end if
 
L

lreames

Below is the entire XML doc from IE that is displayed when
Set xml = Server.CreateObject("Msxml2.DOMDocument.3.0")
xml.setProperty "SelectionLanguage", "XPath"
xml.async = False
xml.loadXML xmldom.xml
Response.write(xml.xml)
is executed.

I am still not getting past
if not nodes is nothing then

AHIA,
Larry....

PS - thanks for your patients. I am new to XML & very very confused at
this point. Can you recommend any books that will point me in the
right direction??

~_~_~_~Start XML~_~_~_~
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><F
xmlns="http://tempuri.org/AGLinkiDataHoster/AGLinkiDataHost"><RetrievePromotionalOpportunitiesListResult>&lt;deals&gt;&lt;escanoffers&gt;&lt;escanoffer
offerid="3743" itemnumber="502022" upc="4300094509" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3747" itemnumber="502030" upc="4300094502" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3989" itemnumber="553824" upc="1600069320" dealamt="0.77"
begindate="03/01/2006" enddate="04/01/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3994" itemnumber="556823" upc="1600066510" dealamt="0.60"
begindate="03/01/2006" enddate="04/01/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="4000" itemnumber="557617" upc="1600068870" dealamt="0.82"
begindate="03/01/2006" enddate="04/01/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="4004" itemnumber="558060" upc="1600067440" dealamt="0.58"
begindate="03/01/2006" enddate="04/01/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="4010" itemnumber="558249" upc="1600064670" dealamt="0.82"
begindate="03/01/2006" enddate="04/01/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3787" itemnumber="502774" upc="4300097199" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3750" itemnumber="502049" upc="4300094511" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3752" itemnumber="502057" upc="4300095019" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3755" itemnumber="502065" upc="4300095022" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3762" itemnumber="502197" upc="4300094520" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3765" itemnumber="502332" upc="4300095077" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3767" itemnumber="502340" upc="4300094963" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3769" itemnumber="502359" upc="4300095085" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3772" itemnumber="502383" upc="4300095016" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3776" itemnumber="502472" upc="4300095009" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3780" itemnumber="502510" upc="4300095025" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3781" itemnumber="502553" upc="4300095023" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /&gt;&lt;escanoffer
offerid="3785" itemnumber="502561" upc="4300094541" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00"
/&gt;&lt;/escanoffers&gt;&lt;/deals&gt;</RetrievePromotionalOpportunitiesListResult></RetrievePromotionalOpportunitiesListResponse></soap:Body></soap:Envelope>
~_~_~_~End XML~_~_~_~
 
B

Bob Barrows [MVP]

Below is the entire XML doc from IE that is displayed when
Set xml = Server.CreateObject("Msxml2.DOMDocument.3.0")
xml.setProperty "SelectionLanguage", "XPath"
xml.async = False
xml.loadXML xmldom.xml
Response.write(xml.xml)
is executed.

I am still not getting past
if not nodes is nothing then

AHIA,
Larry....

PS - thanks for your patients. I am new to XML & very very confused
at this point. Can you recommend any books that will point me in the
right direction??

No. All I've used is the msdn documentation - msdn.microsoft.com/library
~_~_~_~Start XML~_~_~_~
offerid="3743" itemnumber="502022" upc="4300094509" dealamt="0.20"

Something is strange here. Some htmlencoding is occurring somewhere. Is this
really what it looks like? What about when you View Source? Do the &lt;
become < ?
It looks like the xml is really:
<RetrievePromotionalOpportunitiesListResponse><RetrievePromotionalOpportunit
iesListResult><deals><escanoffers>
etc.

So that means the xpath used in the selectnodes statement should be

dim path
path="/RetrievePromotionalOpportunitiesListResponse" & _
"/RetrievePromotionalOpportunitiesListResult/deals" & _
"/escanoffers/escanoffer"
set node = xml.selectnodes(path)

Less efficient but simpler would be:
set node = xml.selectnodes("//escanoffer")
 
L

lreames

Something is strange here. Some htmlencoding is occurring somewhere. Is
this
really what it looks like?
YES

What about when you View Source?
THAT IS WHAT I SENT YOU

Do the &lt; become < ?
NO

Could this be a problem on the host side??

I thought this could be a firewall issue. I ran the script from a box
sitting outside, same result.

My entire script is below. User name & password changed for security.

~_~_~_~Begin Script~_~_~_~
<%@ Language=VBScript %>
<%Response.Buffer=true%>
<%
const SoapServer =
"https://webapps.agbr.com/AGLinkiDataHoster/AGLinkiDataHost.asmx"

set xmldom = server.CreateObject("Microsoft.XMLDOM")
set xmlhttp = server.CreateObject("Microsoft.XMLHTTP")


xmlhttp.open "POST", SoapServer, false
xmlhttp.setRequestHeader "Man" , POST & "
/AGLinkiDataHoster/AGLinkiDataHost.asmx HTTP/1.1"
xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
xmlhttp.setRequestHeader "Content-Length", Len(SoapTest)
xmlhttp.setRequestHeader
"SOAPAction","http://tempuri.org/AGLinkiDataHoster/AGLinkiDataHost/RetrievePromotionalOpportunitiesList"

xmlhttp.send(SoapTest)

if xmlhttp.Status = 200 then '405 means error, 200 means ok. You know
why.

Set xmldom = xmlhttp.responseXML

Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
'Set xml = Server.CreateObject("Msxml2.DOMDocument.3.0")
xml.setProperty "SelectionLanguage", "XPath"
xml.async = False
xml.loadXML xmldom.xml

dim nodes, node, attrs, attr, i
set nodes = nothing
path="/RetrievePromotionalOpportunitiesListResponse" & _
"/RetrievePromotionalOpportunitiesListResult/deals" & _
"/escanoffers/escanoffer"
'set node = xml.selectnodes(path)
set node = xml.selectnodes("//escanoffer")
'set node =
xmldom.selectnodes("/RetrievePromotionalOpportunitiesListResponse/RetrievePromotionalOpportunitiesListResult/deals/escanoffers")

if not nodes is nothing then
for i = 0 to nodes.length - 1
set attrs = node.attributes
for x = 0 to attrs.length-1
set attr=attrs.item(x)
response.write "nodename:" & attr.nodename & "<br>"
response.write "nodevalue:" & attr.nodevalue & "<br>"
next
set node = nodes.nextnode
next
end if

'Set xml = Nothing
Response.write(xml.xml)


Else
Response.Write("Didn't Work<BR>")
Response.Write("status="&xmlhttp.status)
Response.write("<BR>"&xmlhttp.statusText)
Response.Write("<BR>"&Request.ServerVariables("ALL_HTTP"))
End if
set xmlhttp = nothing
set xmldom = nothing
function SoapTest()
SoapTest = SoapTest & "<soap:Envelope
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">"
SoapTest = SoapTest & " <soap:Body>"
SoapTest = SoapTest & " <RetrievePromotionalOpportunitiesList
xmlns=""http://tempuri.org/AGLinkiDataHoster/AGLinkiDataHost"">"
SoapTest = SoapTest & " <Username>ROUSECORP</Username>"
SoapTest = SoapTest & " <Password>agR18!</Password>"
SoapTest = SoapTest & " </RetrievePromotionalOpportunitiesList>"
SoapTest = SoapTest & " </soap:Body>"
SoapTest = SoapTest & "</soap:Envelope>"
end function
%>
~_~_~_~End Script~_~_~_~
 
B

Bob Barrows [MVP]

Something is strange here. Some htmlencoding is occurring somewhere.
Is this
really what it looks like?
YES

What about when you View Source?
THAT IS WHAT I SENT YOU

Do the &lt; become < ?
NO

Could this be a problem on the host side??

I thought this could be a firewall issue. I ran the script from a box
sitting outside, same result.

My entire script is below. User name & password changed for security.


Set xmldom = xmlhttp.responseXML

Why not use xmldom? What is the point in creating another object?
'set node = xml.selectnodes(path)
set node = xml.selectnodes("//escanoffer")
'set node =
xmldom.selectnodes("/RetrievePromotionalOpportunitiesListResponse/RetrievePr
omotionalOpportunitiesListResult/deals/escanoffers")

oops - typo on my part here: "node" should be "nodes" in the previous
statement.

Other than that, I really don't know what is going on.

Does the encoding occur if you do this?

response.contenttype="text/xml"
xmldom.save Response
 
L

lreames

Now, since I added

response.contenttype="text/xml"
xmldom.save Response

the XML displays correctly in IE, but the view source still returns the
same stuff ...&lt;deals&gt;&lt;escanoffers&gt;&lt;...and the script
does not dispaly any node names or values.
 
B

Bob Barrows [MVP]

Now, since I added

response.contenttype="text/xml"
xmldom.save Response

the XML displays correctly in IE, but the view source still returns
the same stuff ...&lt;deals&gt;&lt;escanoffers&gt;&lt;...and the
script does not dispaly any node names or values.

Since it displays correctly in the browser window, could you copy it from
there, cut out the majority of the child nodes and post it here?
 
L

lreames

<?xml version="1.0" encoding="utf-8" ?>
- <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <soap:Body>
- <RetrievePromotionalOpportunitiesListResponse
xmlns="http://tempuri.org/AGLinkiDataHoster/AGLinkiDataHost">

<RetrievePromotionalOpportunitiesListResult><deals><escanoffers><escanoffer
offerid="3743" itemnumber="502022" upc="4300094509" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /><escanoffer offerid="3747"
itemnumber="502030" upc="4300094502" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /><escanoffer offerid="3989"
itemnumber="553824" upc="1600069320" dealamt="0.77"
begindate="03/01/2006" enddate="04/01/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /><escanoffer offerid="3994"
itemnumber="556823" upc="1600066510" dealamt="0.60"
begindate="03/01/2006" enddate="04/01/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /><escanoffer offerid="4000"
itemnumber="557617" upc="1600068870" dealamt="0.82"
begindate="03/01/2006" enddate="04/01/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /><escanoffer offerid="4004"
itemnumber="558060" upc="1600067440" dealamt="0.58"
begindate="03/01/2006" enddate="04/01/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /><escanoffer offerid="4010"
itemnumber="558249" upc="1600064670" dealamt="0.82"
begindate="03/01/2006" enddate="04/01/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /><escanoffer offerid="3787"
itemnumber="502774" upc="4300097199" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /><escanoffer offerid="3750"
itemnumber="502049" upc="4300094511" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00" /><escanoffer offerid="3752"
itemnumber="502057" upc="4300095019" dealamt="0.20"
begindate="03/01/2006" enddate="03/30/2006" returnby="02/28/2006"
selectionlength="14" maxretail="0.00"
/></escanoffers></deals></RetrievePromotionalOpportunitiesListResult>
</RetrievePromotionalOpportunitiesListResponse>
</soap:Body>
</soap:Envelope>
 
L

lreames

The problem is not in the line below...
if not nodes is nothing then
it is in the for line
for i = 0 to nodes.length - 1
because nodes.length = 0
 
B

Bob Barrows [MVP]

The problem is not in the line below...
if not nodes is nothing then
it is in the for line
for i = 0 to nodes.length - 1
because nodes.length = 0

I have tested the following code with the xml you posted (after removing the
space from before the <?xml declaration and the hyphens from the
"expandable" elements.

If this code does not work for you, then you will need to decode the
incoming xml, using the Replace function to replace "&lt;" with "<" and
"&gt;" with ">" and see if that works


<%
dim xmldom, nodes, node, attr, path, xPE, i, strMessage, j
set xmldom=createobject("msxml2.domdocument")
if xmldom.load( server.MapPath("soap.xml")) then
'Response.ContentType="text/xml"
'xmldom.save Response
'path="//deals/escanoffers/escanoffer"
path="/soap:Envelope/soap:Body/" & _
"RetrievePromotionalOpportunitiesListResponse" & _
"/RetrievePromotionalOpportunitiesListResult/deals" & _
"/escanoffers/escanoffer"

set nodes=nothing
set nodes=xmldom.selectNodes(path)
if not nodes is nothing then
if nodes.length>0 then
Response.Write "<table border=""1"" cellspacing=""0"">"
for i = 0 to nodes.length - 1
set node = nodes.nextnode
set attrs = node.attributes
if i = 0 then
Response.Write "<tr>"
for j = 0 to attrs.length-1
set attr=attrs.item(j)
response.write "<th>" & attr.nodename & "</th>"
next
Response.Write "</tr>"
end if
Response.Write "<tr>"
for j = 0 to attrs.length-1
set attr=attrs.item(j)
response.write "<td>" & attr.nodevalue & "</td>"
next
Response.Write "</tr>"
next
Response.Write "</table>"
else
Response.Write path & "did not work"
end if
else
Response.Write path & "did not work"
end if

else
set xPE=xmldom.parseError
strMessage = "errorCode = " & xPE.errorCode & vbCrLf
strMessage = strMessage & "reason = " & xPE.reason & vbCrLf
strMessage = strMessage & "Line = " & xPE.Line & vbCrLf
strMessage = strMessage & "linepos = " & xPE.linepos & vbCrLf
strMessage = strMessage & "filepos = " & xPE.filepos & vbCrLf
strMessage = strMessage & "srcText = " & xPE.srcText & vbCrLf
set xPE = nothing
Response.Write strMessage
end if

%>
 
L

lreames

Bob,

I have put a call in to the vendor, to see what is going on with the
encoding, but I still would like to get this to work.

I created soap.xml & the script above reads it, returning a
nodes.length = 10, so we are reading the file. When set node =
nodes.nextnode is executed, I receive an error ->Object doesn't support
this property or method: 'nextnode'<-, which id do not under stand.

Also, should set attrs = node.attributes be set attrs =
nodes.attributes (nodes with an 's')??

Thanks for all your help, I feel like I am being a real pain.
Larry...
 
B

Bob Barrows [MVP]

Bob,

I have put a call in to the vendor, to see what is going on with the
encoding, but I still would like to get this to work.

I created soap.xml & the script above reads it, returning a
nodes.length = 10, so we are reading the file. When set node =
nodes.nextnode is executed, I receive an error ->Object doesn't
support this property or method: 'nextnode'<-, which id do not under
stand.
The code I posted ran as-is. Perhaps there is a msxml2 version issue. Do you
have MS XML Parser 3.0 installed?

Instead of nextnode, try:

set node = nodes.item(i)

Also, should set attrs = node.attributes be set attrs =
nodes.attributes (nodes with an 's')??

No. We want to be looking at the attributes of the individual node. nodes
contains a list of nodes returned by the selectNodes statement which returns
a list of all the nodes that meet the criteria in the xpath statement.

Bob Barrows
 
L

lreames

Bob,

set node = nodes.item(i), did the trick, I guess MS XML Parser 3.0 is
installed. I am waiting on the vendor to get back with me on the
encoding issue.

Thanks for all your help. If you are ever in New Orleans, look me up,
I owe you a beer.

Larry....
 
B

Bob Barrows [MVP]

Bob,

set node = nodes.item(i), did the trick, I guess MS XML Parser 3.0 is
installed. I am waiting on the vendor to get back with me on the
encoding issue.
So the code does not work with the xml returned from the web service? I was
thinking it might because this code:

response.contenttype="text/xml"
xmldom.save Response

allowed the xml to be parsed by the default xsl style sheet. Also, you
didn't seem to be getting an error when you used loadXML to create a new
domdocument

Oh, well.

You could do something like this:
Set xmldom = xmlhttp.responseXML
Set xml = Server.CreateObject("Msxml2.DOMDocument")
xml.loadXML Replace(Replace(xmldom.xml,"$gt;",">"),"&lt;","<")

and use the xml object instead of the xmldom object.
 

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

Similar Threads

XML and ASP 1
Display XML Element in ASP 3
ASP Using XML Problem 5
ASP, XML stuff... 6
ASP/XML/XSL Sorting Problem 3
xml parse error 0
Trouble with RSS coding - please help! 21

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top