473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

XML value into ASP function

Hi

Possibly a very simple question but how do I get a value out of an XML document so I can play with it in ASP

E.G: <Name>Tom</Name

How do I pull the work tom into asp

Thanks
Jul 19 '05 #1
4 7293
Tim:.. wrote:
Hi,

Possibly a very simple question but how do I get a value out of an
XML document so I can play with it in ASP.

E.G: <Name>Tom</Name>

How do I pull the work tom into asp.

Thanks


Let's make it a little more realistic. Start with this:

dim xmldoc, sName, sID
dim oNameNode
set xmldoc=createobject("msxml2.domdocument")
xmldoc.loadxml "<Names><Name id=""23"">Tom" & _
"</Name><Name id=""24"">Jack</Name></Names>"

This xml document has a root element (documentelement) called Names. This
root element has two child nodes. At this point you have several options:

If you know that the node whose text you wish to read has no child nodes:
1. If you know the location of the node containing the text you wish to
retrieve, then:

sName=xmldoc.documentelement.childnodes(0).text
'sName will contain "Tom"
sName=xmldoc.documentelement.childnodes(1).text
'sName will contain "Jack"

2. If you do not know which of the two nodes has the name "Tom", but you
know the id is 23, then you can use selectSingleNode:

dim oNameNode
'this should be a single line - your newsreader may break it:
set oNameNode = xmldoc.selectSingleNode("/Names/Name[@id='23']")

sName=oNameNode.text

Or, if you know the name "Tom", but wish to retrieve the id:

set oNameNode = xmldoc.selectSingleNode("/Names/Name[.='Tom']")
sID=oNameNode.getAttribute("id")

If there is a possibility that the node has child nodes, such as:
xmldoc.loadxml "<Names><Name id=""23"">Tom" & _
"<Address>13 Test Rd</Address></Name>" & _
"<Name id=""24"">Jack" & _
"<Address>14 Test Rd</Address></Name>" & _
"</Names>"

You can select the proper name node as above, but the text property will not
work well, because this:
sName=oNameNode.text

will contain this:
Tom13 Test Rd

In other words, the text property contains " ... the text content of the
node or the concatenated text representing the node and its descendants."

It is not intuitive, but with this xml: <Name>Tom</Name>, the Name node
actually has a child node whose node type is "text". The value of that node
(its nodeValue) is "Tom". It is as if the structure really was this:
<Name><text>Tom</text></Name>
If you know the structure, i.e., if you know the first child node of the
Name node will always contain the text you want, then you can do this:
sName=oNameNode.childnodes(0).nodevalue

However, if you are not sure of the structure, you will need to loop through
the childnodes until you get to the text node. For example:

xmldoc.loadxml "<Names><Name id=""23"">" & _
"<Address>13 Test Rd</Address>Tom</Name>" & _
"<Name id=""24"">Jack" & _
"<Address>14 Test Rd</Address></Name>" & _
"</Names>"
dim oNameNode,oNode, i
const NODE_TEXT=3

set oNameNode =xmldoc.documentelement.childnodes(0)
for i = 0 to oNameNode.childnodes.length - 1
set oNode=oNameNode.childnodes(i)
if oNode.nodeType= NODE_TEXT then
sName = oNode.nodevalue
exit for
end if
next

HTH,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 19 '05 #2
Hi Bob

Thanks for your response

How do I get the value of an actual childNode if I know the name of the node I want

I tried this but it doesn't work

Thank

Ti

set xml = Server.CreateObject("Microsoft.XMLDOM"
xml.async = fals
xml.load(Server.MapPath("ProjectTEST.xml")

sName=xml.documentelement.childnodes("/Project/Name").tex
response.Write(sName)
Jul 19 '05 #3
Tim:.. wrote:
Hi Bob,

Thanks for your response!

How do I get the value of an actual childNode if I know the name of
the node I want?

I tried this but it doesn't work!
In the future, please explain what "doesn't work" means: error message?
incorrect result?

Thanks

Tim
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("ProjectTEST.xml"))

sName=xml.documentelement.childnodes("/Project/Name").text
response.Write(sName)


If you want to use the name of the node, then you have to use
selectSingleNode.

sName=xml.documentelement.selectsinglenode("/Project/Name").text
You can only use the index number of the childnode if you use the childnodes
collection to get to it.

Bob Barrows

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 19 '05 #4
Tim:.. wrote:
Thanks,

I see what I was doing wrong now!

Is it possible to use selectsinglenode and loop through the whole XML
file, so you return all the Project/Tasks/Task/ID/Name

EG:
<Project><Tasks><Task><ID>1</ID><Name>Pro1</Name></Task><Task><ID>2</ID><Nam
e>Pro2</Name></Task></Tasks></Project>
.:CODE

Something like this perhaps ?

FOR i = 0 to (xml.documentelement.selectsinglenode.length -1)
sName=xml.documentelement.selectsinglenode.Item(i) ("/Project/Tasks/Task/Star
t").text NEXT

Thanks again!


It sounds like you need to use selectNodes:

oNodes=xml.selectNodes("/Project/Tasks/Task/Start")
for each oNode ion oNodes
sname=oNode.text
next

Bob Barrows

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
1
by: koen colen | last post by:
Hello group, I hope you guys can help me out, I am modifying a piece of code from Joe Norman, I did found the code here: http://www.intranet2internet.com/public/default.asp?PAGE=sscript&ID=3 ...
16
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not...
8
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
2
by: joltman | last post by:
OK, this is kind of hard to explain, so I'll do my best: I have a form where I have a row where there could be multiple entries, so I have a link where it will dynamically add another row like it,...
21
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
2
by: mndprasad | last post by:
Hi friends, Am new to AJAX coding, In my program am going to have two texbox which going to implent AJAX from same table. One box is going to retrieve the value of other and vice...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.