对于这个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>
这是我的代码
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
这是我的输出
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
有人能帮我调整一下吗,让它每个条目只返回一个图像
我并不精通VB6,但从XPath的角度来看,我会选择相对于您已经拥有的xmlEntryNode:
Set xmlC1Nodes = xmlEntryNode.selectNodes("media:group/media:thumbnail[1]/@url")
(我不明白为什么你的XPath表达式没有引号……但你说它会产生输出……)
或者,如果selectNodes()方法仅适用于XML文档,则
Set xmlC1Nodes = xmlDoc.selectNodes(
"/*/x:entry[" + i + "]/media:group/media:thumbnail[1]/@url")
有人能帮我调整一下吗,让它每个条目只返回一个图像
使用类似的东西(多年来一直没有使用MSXML或VB):
Set urlAttrib = xmlEntryNode.SelectSingleNode("media:group/media:thumbnail[1]/@url")
wantedText = urlAttrib.nodeValue