I have the following multi-level XML tree that I am trying to parse. I can easily get the first level, but cannot seem to figure out how to get the others after much searching. Ultimately what we need is a full list of the data in each level iterating over each article as well as grabbing the article id. Can someone assist me please?
XML:
<search>
<numresults>26707</numresults>
<pagesize>20</pagesize>
<articles>
<article id="998">
<title>my title 1</title>
<url>www.google.com</url>
<attributes>
<performer>
<id>122</id>
<url>www.yahoo.com</url>
<name>Elvis</name>
</performer>
</attributes>
</article>
<article id="999">
<title>my title 2</title>
<url>www.microsoft.com</url>
<attributes>
<performer>
<id>123</id>
<url>www.aol.com</url>
<name>Aerosmith</name>
</performer>
</attributes>
</article>
</articles>
</search>
Here is what I have that will work for the XML to get level one:
Set objXML = CreateObject("Microsoft.XMLDOM")
Set objEvents = CreateObject("Microsoft.XMLDOM")
objXML.async = False
objXML.setProperty "ServerHTTPRequest", true
objXML.Load (sURL)
'CaSe sensitive
Set objEvents = objXML.getElementsByTagName("article")
NumEvents = objEvents.length 'zero based
Response.Write NumEvents
c = 0
Do Until c = NumEvents
Set objEventDetails = objEvents.item(c)
Title = Trim(objEventDetails.childNodes(0).text)
Response.Write Title & "<br>"
c = c + 1
Loop 'main XML feed loop
Set objEvents = Nothing
Set objXML = Nothing
Any help would be greatly appreciated!!
Use XPath instead of crawling next child.
Here's an example
Set xml = CreateObject("Msxml2.DOMDocument")
xml.Async = "False"
xml.Load("search.xml")
strReturn = ""
for each ndArticle in xml.selectNodes( "//article" )
strReturn = strReturn + ndArticle.getAttribute( "id" ) + vbCrLf
for each ndChild in ndArticle.selectNodes(".//" )
if ndChild.nodeName <> "#text" then
strLast = ndChild.nodeName
else
strReturn = strReturn + strLast + "=" + ndChild.nodeTypedValue + "<br/>" + vbCrLf
end if
next
next
WScript.Echo strReturn
It creates
998
title=my title 1<br/>
url=www.google.com<br/>
id=122<br/>
url=www.yahoo.com<br/>
name=Elvis<br/>
999
title=my title 2<br/>
url=www.microsoft.com<br/>
id=123<br/>
url=www.aol.com<br/>
name=Aerosmith<br/>
If anyone else has this issue, this is how I solved it. See this post:
how to solve "The download of the specified resource has failed" error?
The last comment at the bottom worked for me. There was an issue calling my URL from the same directory as the script (i know, totally forgot - rookie mistake), that then threw the other error of "The download of the specified resource has failed" leading me to this solution.
I think that there my be a security feature on my server blocking William's code from working above....
thanks for the help William!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.