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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp and xml 1

Status
Not open for further replies.

onressy

Programmer
Mar 7, 2006
421
CA
I have an xml being sent to page.asp, this xml contains username and password that i need to extract before processing into a database. But before i can do all that I'm wondering how i can begin to use the xml when it is post from an external server to mine, do i need to create an object to pass the xml file into?

I was reading and found this:

set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
xmldoc.load(Request)

I not sure if i need the above and if so how to really use it. Will someone please explain the jist of how a remote server sends an xml file and how one can begin to use this xml to retrieve the username and password.

This is part of the schema that contains the username and password:
Code:
<?xml...
<prop xmlns:xsi="[URL unfurl="true"]http://www.w3.....[/URL]
  <code chainID="RK" companyID="DS" propertyID="1234">
       <user username="test" password="test />
  </code>
  <rates>
       <rate start=......
</prop>

Thanks a bunch.
 
Ok, the part you have is definately what you want in order to pull the XML data into an object. At this point you get to learn about all kind of nifty stuff, like DOM and XPath :)

W3Schools has some tutorials on both:

You basically have two ways to retrieve the data that you need. You can either navigate the DOM tree of the XML by referencing child nodes or you can use XPath statements inside a SelectNodes (or SelectSingleNode) to retrieve the exact node you need from wherever it is in the document.


I prefer XPath queries, for the simple fact that I find it easier to maintain the code when I see text instead of chained .childNodes(0).childNodes(0).etc


A simple example of getting the username and password using XPath:
Code:
set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
xmldoc.load(Request)

Dim user_name, user_pass
user_name = xmldoc.SelectSingleNode("/prop/code/user@username").Value
user_pass = xmldoc.SelectSingleNode("/prop/code/user@password").Value

Now, for the sake of an example, if we had multiple rate records to process in the rates section, we could handle that like so:
Code:
Dim rate, ratelist
Set ratelist = xmldoc.SelectNodes("/prop/rates/rate")

'process each element in this list
'   lets pretend that rate has:
'      attributes: start, end (optional), price
'      children: optional name tag for the "Super Special Summer Saving Rate" or whatever
'
' <rate start="123" end="123" price="123">
'   <name>whatever</name>
' </rate>
Dim rate_name, rate_start, rate_end, rate_price
For Each rate in ratelist
   rate_start = rate.attributes.getNamedItem("rate_start").Text
   If Not isNull(rate.attributes.getNamedItem("rate_end")) Then
      rate_end = rate.attributes.getNamedItem("rate_end").Text
   End If
   rate_start = rate.attributes.getNamedItem("rate_price").Text

   If rate.hasChildNodes() Then
      rate_name = rate.childNodes(0).childNodes(0).Text
   End If

   'Do something with the data

Next

Now an explanation. Elements do have a getAttribute() function that would have been shorter than using the attributes node list, but I wanted to keep the lines consistent. With DOM there is always about 3 differant ways to do things, sometimes because MS has added functions to their DOM that others don't have in an attempt to make it easier for us.

Everything is a node. Obvious your XML elements are a node, but so are your attributes, the text inside your elements, everything.
So when we pulled back an attribute what we really wanted was the text from inside that attribute node. However, when we pulled in our fictional "name" element we had to then get it's first child (a text node) and then get the text from inside that node. I remember this confused the hell out of me for years, but as long as you remember that everything is a node, even text, then that will help.

Next, I shortcut a little by just checking if there were any children in the rate element and then assuming if there were it was the name tag. Since my exmaple only had the one possibility tat would normally work out just fine. In a situation where you had multiple required and/or optional elements inside the rate element, that obviously would not work so well. You have the option of using selectNode, looping through childNodes, and several others in order to find out if your optional elements are there.
Watever you do, don't do anything like:
myvar = rates.getSingleNode("name").childNodes(0).text
because ASP will blow up in your face the first time you run into a "rate" element with no "name" element. Apparently null doesn'thave a method or collection called childNodes :)


In any case, hopefully this will be enough to get you started. If you have any further quetions definately post back and we'll see what w can do.

-T

signature.png
 
Just a minute detail.
[tt] user_name = xmldoc.SelectSingleNode("/prop/code/user[red]/[/red]@username").Value
user_pass = xmldoc.SelectSingleNode("/prop/code/user[red]/[/red]@password").Value[/tt]
 
Wow Tarwn, that you very very very much for the detailed explaination. Better that anything i could find on the net.

Thanks!
 
NOTE: thanks tsuji for the oversight

OK i've setup the ASP with error handling, and there are none:
If (xmldoc.parseError.errorCode <> 0) Then

i also made sure the I had the right path to the xml packet:
response.write("The path to the xml file is: " & _
Server.MapPath("onRes2.xml") & "<br />")

All is well except the following does not return a value:
user_name = xmldoc.SelectSingleNode("/prop/code/user/@username").Value

any suggestions?

Thanks

Code:
Set xmldoc = Server.CreateObject("Microsoft.XMLDOM")

xmldoc.async = False

xmldoc.load (Server.MapPath("onRes2.xml")) 

'make sure we have the right path to the xml packet
response.write("The path to the xml file is: " & _
Server.MapPath("onRes2.xml") & "<br /><br /><br />")

[code]
If (xmldoc.parseError.errorCode <> 0) Then 
   Response.Write "XML " & strType & " error - " & "<br>" & _
  				  "Error Reason: " & xmldoc.parseError.reason & "<br>" & _
  				  "Source: " & xmldoc.parseError.srcText & "<br>" & _
  				  "Error Line: " & xmldoc.parseError.line & "<br>" & _
 			      "Error Position: " & xmldoc.parseError.linepos & "<br>" 
   Response.End 
Else 

strUsername  = xmldoc.SelectSingleNode("/prop/code/user/@username").Value

response.write strUsername

End If

Set objXML = Nothing 
%>
 
I forgot to post what the error was:

Microsoft VBScript runtime error '800a01a8'
Object required: 'SelectSingleNode(...)'
 
got it figured:
Set objNode = objXML.SelectSingleNode("/property/code/user/@username")
If not objNode Is Nothing Then
strDayTemp = objNode.text
Set objNode = Nothing
End If

thanks!
 
Sorry for the error above (nice catch tsuji) I don't usualy have my windows box on, so I'm genrally typing from memory, and I don't have much of that :)

You may want to consider adding some validatin to your page if you plan onopening it up to anyone other than that other script you were bulding. I would suggest doing either a DTD or Schema so you can validate that they are sending you at least x as a bare minimum, that the tags are nested as your expecting them, content is the right type, etc.

signature.png
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top