• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

Reference XML node by name

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
periker
Asked:
periker
  • 6
  • 2
1 Solution
 
kevp75Commented:
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
 
kevp75Commented:
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
 
perikerAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
kevp75Commented:
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
 
kevp75Commented:
hold on...gimme a sec to throw up a test page
0
 
kevp75Commented:
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
 
perikerAuthor Commented:
Perfect! Thanks!!
0
 
kevp75Commented:
anytime
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now