Solved

Reference XML node by name

Posted on 2009-05-05
8
356 Views
Last Modified: 2012-05-06
How do you reference an xml node by name? I want to reference "author" specifically by name.
<document>
  <item>
    <title>Chief</title>
    <author>Larry X.</author>
    <price>45.00</price>
  </item>
 
  <item>
    <title>Captain</title>
    <author>Larry X.</author>
    <price>59.00</price>
  </item>
</document>
 
 
Set xmlObj = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xmlObj.async = False
xmlObj.setProperty "ServerHTTPRequest", True
xmlObj.Load("http://domain.com/xml.xml")
If xmlObj.parseError.errorCode <> 0 Then
	Response.Write "Sorry, newsfeed is unavailable"
End If
Set xmlList = xmlObj.getElementsByTagName("item")
Set xmlObj = Nothing
For Each xmlItem In xmlList
	Response.Write "<br>"
	Response.Write xmlItem.getElementsByID("author") & "<br>"
Next
Set xmlList = Nothing

Open in new window

0
Comment
Question by:periker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 25

Expert Comment

by:kevp75
ID: 24314530
try:

Set xmlObj = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xmlObj.async = False
xmlObj.setProperty "ServerHTTPRequest", True
xmlObj.Load("http://domain.com/xml.xml")
If xmlObj.parseError.errorCode <> 0 Then
	Response.Write "Sorry, newsfeed is unavailable"
End If
Set xmlList = xmlObj.getElementsByTagName("item")
Set xmlObj = Nothing
For Each xmlItem In xmlList
	Response.Write "<br>"
	Response.Write xmlItem.getElementsByTagName("author").text & "<br>"
Next
Set xmlList = Nothing

Open in new window

0
 
LVL 25

Expert Comment

by:kevp75
ID: 24314539
or:

Set xmlObj = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xmlObj.async = False
xmlObj.setProperty "ServerHTTPRequest", True
xmlObj.Load("http://domain.com/xml.xml")
If xmlObj.parseError.errorCode <> 0 Then
	Response.Write "Sorry, newsfeed is unavailable"
End If
Set xmlList = xmlObj.getElementsByTagName("item")
Set xmlObj = Nothing
For Each xmlItem In xmlList
	Response.Write "<br>"
	Response.Write xmlItem.Item(1).text & "<br>"
Next
Set xmlList = Nothing

Open in new window

0
 

Author Comment

by:periker
ID: 24316664
Your first options doesn't work
Object doesn't support this property or method: 'text'

Your second option won't work in my case because the fields may be in different orders depending on the xml file I am retrieving.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:kevp75
ID: 24317489
ok.  try this:


Set xmlObj = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xmlObj.async = False
xmlObj.setProperty "ServerHTTPRequest", True
xmlObj.Load("http://domain.com/xml.xml")
If xmlObj.parseError.errorCode <> 0 Then
	Response.Write "Sorry, newsfeed is unavailable"
End If
Set xmlList = xmlObj.getElementsByTagName("item")
For i = 0 To xmlList.ChildNodes.Length - 1
	Response.Write "<br>"
	Response.Write xmlList.ChildNodes.Item(i).getElementsByTagName("author").text & "<br>"
Next
Set xmlList = Nothing
Set xmlObj = Nothing

Open in new window

0
 
LVL 25

Expert Comment

by:kevp75
ID: 24317499
hold on...gimme a sec to throw up a test page
0
 
LVL 25

Accepted Solution

by:
kevp75 earned 50 total points
ID: 24317711
here ya go:

test page: http://www.zipcm.com/temp.asp

code:

<%
Dim strXML : strXML = "<document><item><title>Chief</title><author>Larry X.</author><price>45.00</price></item><item><title>Captain</title><author>Larry X.</author><price>59.00</price></item></document>"
Set xmlObj = Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
	xmlObj.async = False
	xmlObj.LoadXML(strXML) 'change back to .Load("http://domain.com/xml.xml")
	Set xmlList = xmlObj.selectNodes("//document")
		'Response.Write(IsObject(xmlList) & ":" & IsNull(xmlList))
		If IsObject(xmlList) And Not(IsNull(xmlList)) Then
			For Each objNode In xmlList
				For i = 0 To objNode.ChildNodes.Length - 1
					Set itemNode = objNode.ChildNodes(i)
						Response.Write("<strong>Author:</strong> " & itemNode.selectSingleNode("author").text & "<br />")
					Set itemNode = Nothing
				Next : i = Null
			Next : objNode = Null
		Else
			Response.Write("There are no items.")
		End If
	Set xmlList = Nothing
Set xmlObj = Nothing
%>

Open in new window

0
 

Author Closing Comment

by:periker
ID: 31578183
Perfect! Thanks!!
0
 
LVL 25

Expert Comment

by:kevp75
ID: 24319112
anytime
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Scheduled IIS .Net2 AppPools recycle and SQL connection Hangs 33 148
Filktering Alphabetically 8 41
Sending ASP to server side 8 31
Session on Html 8 39
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question