Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
for each entry in this xml, i need to get the "title" and the first thumbnail image. that is the first image in the media:group


XML
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/'>
<entry>
<title>Progression! 9 more pounds to goo!</title>
<media:group>
<media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/default.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/mqdefault.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/hqdefault.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/1.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/2.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/3.jpg'/>
</media:group>
</entry>
<entry>
<title>Plank Variations Workout with Max Wettstein</title>
<media:group>
<media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/default.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/mqdefault.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/hqdefault.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/1.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/2.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/3.jpg'/>
</media:group>
</entry>
</feed>



this is my code


VB
Dim xmlDoc As MSXML2.DOMDocument30
Dim xmlEntryNode As MSXML2.IXMLDOMNode
Dim xmlEntryNodes As IXMLDOMNodeList
Dim xmlC1Nodes As IXMLDOMNodeList
Dim ns As String
Set xmlDoc = New DOMDocument30
ns = txtNS.Text
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.setProperty "SelectionNamespaces", ns

If xmlDoc.loadXML(txtXml.Text) = False Then
	appendText "xml document load failed"
	Exit Sub
End If
Set xmlEntryNodes = xmlDoc.documentElement.selectNodes(/x:feed/x:entry)
Dim i As Integer
For i = 0 To xmlEntryNodes.length - 1
	Set xmlEntryNode = xmlEntryNodes(i)
	appendText xmlEntryNode.Text
	Set xmlC1Nodes = xmlDoc.selectNodes(//media:group/media:thumbnail[1]/@url)
	If xmlC1Nodes.length > 0 Then
		Dim j As Integer
		For j = 0 To xmlC1Nodes.length - 1
			appendText xmlC1Nodes(j).Text
		Next
	End If
Next

Exit Sub



and here is my output


Progression! 9 more pounds to goo!	
http://i.ytimg.com/vi/7MTjYXt3rLQ/default.jpg	
http://i.ytimg.com/vi/O1Nd8lZFGpc/default.jpg	 
Plank Variations Workout with Max Wettstein	
http://i.ytimg.com/vi/7MTjYXt3rLQ/default.jpg	
http://i.ytimg.com/vi/O1Nd8lZFGpc/default.jpg




can anyone pls help me adjust to make it return only one image for each **entry**
Posted

1 solution

What happens is that you select in the inner for-next loop all the first thumbnails of each media:group node. Instead you only want the first media:thumbnail of the current item in de outer for-next loop.

You can solve this by changing the line with 'Set xmlC1Nodfes =' to:
VB
Set xmlC1Nodes = xmlEntryNode.selectNodes("media:group/media:thumbnail[1]/@url")


Two things are changed in this line. In the posted code you used xmlDoc.selectNodes, thus referencing the whole XML document. Changing this into the xmlEntryNode.selectNodes, will give you a reference to the current entry.

This will not solve the problem, beause off the trailing '//' in your XPath query (searching from the rootnode of the document). Remove these to start your XPath query from the xmlEntryNode.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900