Retrieve Element from Sub-Child in ASP

D

david.n.holley

I am building an application that retrieves data from an external XML
page, and I need to retrieve an important variable that is not outside
of the root name.

For instance, say I want to retrieve the amount (3) from
"Ingredient" (see below) - since it isn't outside of the root name,
how can I achieve this??

<title>Sue's Good Pie</title>
<date>January 3, 2007</date>
<makes>1 Pie</makes>
<ingredient amount="3" unit="cups">Flour</ingredient>
<instructions>
<step>1. Blend 6 Rhubarbs for 3 Minutes.</step>


If you can point me to some resources that can explain this, I'd
greatly appreciate it.


If you're interested in looking at my code for the ASP page, its
posted below:
<%Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument")
xmlDOM.async = False

xmlDOM.setProperty "ServerHTTPRequest", True
xmlDOM.Load("thisrecipe.xml")

'Get all of the <item> tags in the feed
Set itemList = xmlDOM.getElementsByTagName("recipe")

strHTML = strHTML & ""

'Iterate over each item
For Each item In itemList

'Parse the item children
For each child in item.childNodes
Select case lcase(child.nodeName)
case "title"
title = child.text
case "submittedby"
submittedby = child.text
case "makes"
makes = child.text
case "date"
datesubmitted = child.text
End Select
Next %>
 
J

Joe Kesselman

I don't do ASP programming, but it sounds like you're looking for a
basic introduction to the Document Object Model (DOM) APIs (if you want
to program this explicitly) or XPath (if your environment includes an
XPath engine). Find the ingredient element, find its amount attribute,
retrieve the value of same.

Of course what you've shown us isn't a well-formed XML document -- no
single root element, and you didn't close instructions -- but I'm
assuming that's because it's just an illustrative fragment.
 
M

Martin Honnen

For instance, say I want to retrieve the amount (3) from
"Ingredient" (see below) - since it isn't outside of the root name,
how can I achieve this??
<ingredient amount="3" unit="cups">Flour</ingredient>

amount is an attribute of the ingredient element.

<%Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument")
xmlDOM.async = False

xmlDOM.setProperty "ServerHTTPRequest", True
xmlDOM.Load("thisrecipe.xml")

xmlDOM.setProperty "SelectionLanguage", "XPath"

Set amount = xmlDOM.selectSingleNode("//instruction/@amount")
If Not amount Is Nothing Then
Response.Write "amount is " & amount.value
End

The XPath //instruction/@amount is just an (inefficient) example, in
reality you might want to use a better path as you know the ancestor
elements of instruction.
 
D

david.n.holley

You're right, its not the most efficient way, but it does get the job
done. Thanks for providing it in ASP (speaking of inefficient...)

And the XML sheet I provided as an example isn't really what I'm using
(the one that I provided wasn't a complete file).

Thanks for the help!
 
A

Andy Dingley

I am building an application that retrieves data from an external XML
page,

Apart from the XML parsing details, you should be very careful with
that as a technique. I just wouldn't do it, at that simple level. You
need to add some level of caching for "production" code.

If it works, then it's a slow page - your server-side code has to go
off and retrieve something external every time.

If it works, then it hammers the remote server. They might object to
this, they could well ban or throttle your access (even to a "public"
RSS feed).

It often won't work. If the remote server is down or just slow, your
page will appear to have failed too.

What you're building here is an "aggregator" (you can search for that
term), and quite possibly close to an RSS aggregator. Most of those
separate the loading and serving of remote feeds, and cache via a
database in the meantime. This provides a better local service, it
limits the remote load, and it also provides some robustness when the
remote service goes down.

I'd also question whether it's a good idea to have "Menu XML" in this
way, or if you'd be better fitting it into the overall RSS framework,
possibly as an RSS 1.0 Module. A simpler way would be to use "Menu
XML", but just to embed this within Atom (or RSS 2.0) to handle the
syndication aspects.
 

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,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top