Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading XML query in ASP 2

Status
Not open for further replies.

cranger01

IS-IT--Management
Oct 2, 2006
34
US
Question. I am trying to access XML data from a HTTP query from a remote site in ASP. I have found examples, for things close to what I am doing, but nothing exact and seem to be missing a piece.

If I type in a URL I get the xml query results back. Here is an example but the link and data slightly altered for security.


This returns

- <Root xmlns:sql="urn:schemas-microsoft-com:xml-sql">
- <Store regionID="4" storeID="44" storeCode="444" name="My Store 1" >
</Store>
- <Store regionID="5" storeID="55" storeCode="555" name="My Store 2 >
</Store>
</Root>

Now I am trying to retrieve this in ASP, but get an error on the Load Statement.


Set objXML = Server.CreateObject("Microsoft.XMLDOM")
Set objLst = Server.CreateObject("Microsoft.XMLDOM")
Set objHdl = Server.CreateObject("Microsoft.XMLDOM")

objXML.async = False
objXML.Load (" If objXML.parseError.errorCode <> 0 Then
Response.write (objXML.parseError.errorCode)
End If

So basically, I am not sure how to load the data from the query.... If someone can point me in the write direction, that would be awesome.
 

Try using ServerXMLHTTP to load the remote file, then parse with loadXML method of your XML document object.

e.g.:
[tt]
oXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")

oXMLHTTP.open ( "GET", "url_here",false)
oXMLHTTP.send

oXMLDoc.async = false
oXMLDoc.loadXML(oXMLHTTP.responseXML.xml)
[/tt]

etc..

A smile is worth a thousand kind words. So smile, it's easy! :)
 
damber,

Thanks for you help. Sorry, I left off the ".

I tried your syntax and got the below error. Am I missing a definition somewhere? FYI, I am not an ASP guru...more of a DBA working on a proof of concept.

Error Type:
Microsoft VBScript compilation (0x800A0414)
Cannot use parentheses when calling a Sub
/xxx/xx/xxx/xxx xxxxx/xxxxxxxx/testform.asp, line 17, column 83
oXMLHTTP.open ("GET", "
 

oops, remove the ( ) bits

A smile is worth a thousand kind words. So smile, it's easy! :)
 
oXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")

Error Type:
Server object, ASP 0177 (0x800401F3)
Invalid class string


Before we continue this, I am running this on my local machine using IIS that comes with Windows XP. Do I need to load/install anything?
 

try:
oXMLHTTP = Server.CreateObject("MSXML.ServerXMLHTTP")

It depends on the version you have installed, but the above should cover most set-ups

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Another option would be to not try and force the MSXML verison, like so:
Code:
Dim oXMLHTTP
Set oXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")

You can't always be sure what version is the default for the system, but it has worked consistently for me since generally you should have v2.x, 3, 4, and maybe even 5 and they all support the basic funcitonality you need.

 
The op's original script as presented by itself should be functional. Only you've to correct some typos.

><Store regionID="5" storeID="55" storeCode="555" name="My Store 2 >
[tt]<Store regionID="5" storeID="55" storeCode="555" name="My Store 2[highlight]"[/highlight] >[/tt]

> objXML.Load ("[tt] objXML.Load ("[highlight]"[/highlight])[/tt]

The functioning of it takes a tacit "contract" between the client and the server of proper configuration. On the server-side, it is understood that the xml source document (as shown) is being served as a well-formed xml document text stream.
 
Generally i would agree, but I have two reservations:
1) With XMLHTTP you fore a step between retrieval and parsing, which makes for easier testing
2) I have had some odd issues with XMLDOM.Load() not working consistently on all URL's i have fed it

-T

 
Thanks all...

disreguard the typo's...the " were left off.

Well, starting to feel like an idiot. This is what I have now. I just got in and read the threads, so I am going to play around with this for a bit....just wanted to put up a post so those helping are aware where I am.

Dim oXMLHTTP
Set oXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
oXMLHTTP.open "GET", "oXMLHTTP.send
oXMLDoc.async = false

Last line listed errors with:

Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required: 'oXMLDoc'
 
Where is it defined, oXMLDoc? If you eventually succeed in working with xmlhttp, you should have more solid understanding of scripting than that. Isn't it obvious you have not defined/instantiate oXMLDoc object?

Besides, if you fail to work out the original script---and to my estimation, it should work modulo the "contract" being respected by the client and server alike---which I suppose now you get it somewhere, despite spurious typos, any metamorphos using similar control will fail for the same/similar reason.
 
Thanks All who helped. I got it to work, just need to pull the data I need, but I am reading through the data.
Also, sorry for my first post this morning, I just jumped in and tried it before I really thought about things and drank my coffee.

Final Look.

Dim oXMLHTTP
Set oXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
set oXMLdoc = Server.CreateObject("Microsoft.XMLDOM")
Set objLst = Server.CreateObject("Microsoft.XMLDOM")
Set objHdl = Server.CreateObject("Microsoft.XMLDOM")
oXMLHTTP.open "GET", "oXMLHTTP.send
oXMLDoc.async = false
oXMLDoc.loadXML(oXMLHTTP.responseXML.xml)


If oXMLDOC.parseError.errorCode <> 0 Then
Response.Write oXMLDoc.parseError.errorCode
End If

Set objLst = oXMLDoc.getElementsByTagName("Store")
noofStores = objLst.length
%>
 
Sorry, I have one more issue...With all the above logic as true....

Why am I still getting an invalid obj error on line above Next. Took this code snippit from 15 seconds.

Response.Write (noOfStores)
For i = 0 To (noOfStores - 1)
Response.Write (i)
Set objHdl = objLst.item(i)
Response.Write objHdl.childNodes(1).childNodes(0).text
Next
 
crange01,

Try this way:

set oDocRoot = oXMLDoc.documentElement
set oStoreNodes = oDocRoot.selectNodes("//Store")

for each oStore in oStoreNodes
response.write server.htmlencode(oStore.xml)
response.write "<br/><br/><br/>" & oStore.childNodes(1).childNodes(0).text
next

However, the second line may not provide anything - you need to make sure that the structure in the XML fits with what you're asking it to do.. and make sure you understand XML's terminology and relationships between parent, child, subling and how text content works (read this link where it says IMPORTANT:
Also, remember to dim ALL your variables - to make sure you do this, use Option Explicit at the top of your ASP page.

You also don't need to define the objHdl and objLst variables explicitly (e.g. the CreateObject statements) - this will be done during assignment


For help with XML look at these links:

And for ASP:

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Thanks again. Sorry for being such a pain. I got it to work the following way. This will work for what I need to do. Thanks again everyone for the help.

For i = 0 To (noOfStores - 1)

If objLst.item(i).nodeName = "Store" Then
set objHdl = objLst.item(i)
storename = objHdl.attributes.getNamedItem("name").nodeValue
storeaddr = objHdl.attributes.getNamedItem("address1").nodeValue
.
.
.
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top