Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to loop childnodes?

Status
Not open for further replies.

hakkatil

ISP
Apr 4, 2003
10
US
I need to loop childnodes in xml page.

My XML page has:
<story>
<category>A</category>
<subcategory></subcategory>
<keyword>Senate Joyce</keyword>
<storyname>129910</storyname>
<locator>FED</locator>
<source>AAP</source>
<headline>Nationals contender edges ahead</headline>
<byline></byline>
<dateline>CANBERRA</dateline>
<abstract>Barnaby Joyce, the Queensland Nationals Senate candidate vying for the state's sixth and final Senate spot, is confident that he will make it across the line.</abstract>
<bodytext>
<p>Barnaby Joyce, the Queensland Nationals Senate candidate vying for the state's sixth and final Senate spot, was guardedly confident that he'd make it across the line.</p>
<p>Mr Joyce said he had a big scare in counting on Friday when it appeared he had lost his lead to Greens candidate Drew Hutton in the poll for the seat of Maranoa, which had been held for the Nationals by Bruce Scott.</p>
<p>"We are ahead again but you never count it in until you are there, Mr Joyce told ABC television.</p>
<p>"We are about 3,000 votes ahead but we have got to wait for that magic day where they punch the button that sorts out the below the lines votes and that will be on Wednesday.</p>
<p>"We just hope and pray and till then that we stay in this position and stay ahead."</p>
<p>Mr Joyce said he was a small-town country accountant who grew up on the land in a place called Danglemar which no longer exists.</p>
<p>He said he was a loyal party man and the Queensland Nationals provided the riding orders.</p>
<p>"We are ready to work with a whole heap of key industry groups and key groups and we are going to be very responsible, but we are going to go into bat for Queensland," he said.</p>
<p>Mr Joyce admitted he had been critical of Prime Minister John Howard for sending out a flyer in Queensland promoting the Liberals.</p>
<p>"Obviously we are very close to the wire and it could have been a bit easier for us if that hadn't gone out," he said.</p>
<p>The Liberals and Nationals appear to have won four of six Queensland Senate seats and Mr Joyce said that reflected energetic and effective campaigning.</p>
<p>"We campaigned very hard. We went up and down the state six times," he said.</p>
<p>"We were just going flat out under the radar."</p>
<p>Mr Joyce said talk in the pubs and clubs and the taxi driver test indicated Opposition Leader Mark Latham was going to have a hard time.</p>
<p>He said labor's Tasmanian forests policy and the moves against forestry workers blew him out of the water.</p>
</bodytext>
<sidebar></sidebar>
<sidehead></sidehead>
<date>24/10/2004 11:30:50 AM</date>
</story>

MY ASP page:

<% Set objXML = Server.CreateObject("Microsoft.XMLDOM")

objXML.Load (Server.MapPath("justinxml.xml"))
objXML.async=false
'set xsl = Server.CreateObject("Microsoft.XMLDOM")

'xsl.load(Server.MapPath("style.xsl"))
'Response.Write(objXML.transformNode(xsl))

If objXML.parseError.errorCode <> 0 Then
Response.Write "<p><font color=red>Error loading the Resource file.</font></p>"
Response.End
End If
Set objLst = objXML.getElementsByTagName("story")
' for each resource in the list
For i = 0 To 4'objLst.Length - 1

Set subLst = objLst.item(i)

Response.write "<P>" & vbcrlf
Response.Write "<a href=""edit_finished.asp?url="& i &""">" & _
subLst.childNodes(6).childNodes(0).text & "</a><br>"
Response.Write "</p>"

Next
dim url
if request.QueryString("url") <> "" then
url = request.QueryString("url")
Set subLst = objLst.item(url)
Response.write "<P>" & vbcrlf
Response.Write subLst.childNodes(0).childNodes(0).text & "<br>"
Response.Write subLst.childNodes(2).childNodes(0).text & "<br>"
Response.Write subLst.childNodes(3).childNodes(0).text & "<br>"
Response.Write subLst.childNodes(4).childNodes(0).text & "<br>"
Response.Write subLst.childNodes(5).childNodes(0).text & "<br>"
Response.Write subLst.childNodes(6).childNodes(0).text & "<br>"
'Response.Write subLst.childNodes(7).childNodes(0).text & "<br>"
Response.Write subLst.childNodes(8).childNodes(0).text & "<br>"
Response.Write subLst.childNodes(9).childNodes(0).text & "<br>"
Response.Write "- " & subLst.childNodes(10).childNodes(0).text & "<br>"
Response.Write "- " & subLst.childNodes(10).childNodes(1).text & "<br><br>"
Response.Write "- " & subLst.childNodes(10).childNodes(2).text & "<br><br>"
Response.Write "- " & subLst.childNodes(10).childNodes(3).text & "<br><br>"
Response.Write "- " & subLst.childNodes(10).childNodes(4).text & "<br><br>"
Response.Write "- " & subLst.childNodes(10).childNodes(5).text & "<br><br>"
Response.Write "- " & subLst.childNodes(10).childNodes(6).text & "<br><br>"
Response.Write "- " & subLst.childNodes(10).childNodes(7).text & "<br><br>"
Response.Write "- " & subLst.childNodes(10).childNodes(8).text & "<br><br>"
Response.Write "- " & subLst.childNodes(10).childNodes(9).text & "<br><br>"


Response.Write subLst.childNodes(13).childNodes(0).text & "<br>"

Response.Write "</p>"
end if
Set xsl = nothing
Set objXML = nothing
Set objLst = nothing

%>

I need to loop red lines above.

The above code displays all the nodes. But my problem starts when I try to display bodytext tag. Because bodytext has different amount of childs. Sometimes 7 sometimes 15 child.

How can I loop bodytext's childs in asp? I tried many way but no luck.

Thank you
 
childNodes should have a Length property, and you can use a for..next loop to go thru them all.

for i = 0 to mynode.childnodes.length
Response.Write mynode.childnodes(i).Text
next i

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thank you for the answer.

But it did not work.

It asks an object. if I type a number instead of i, it works. Like:

Response.Write mynode.childnodes(10).Text
instead of
Response.Write mynode.childnodes(i).Text
 
If you're in VB, you can do this:

Dim Item as IXMLDOMNode
For Each Item in mynode.childNodes
Response.Write Item.Text
Next

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thank you.

But Response.Write Item.Text displays all childs as one line. I need to display every child on separate line. Is this possible?
 
Never mind. It is working now. Thank you very much chiph.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top