VBA Excel MSXML2.XMLHTTP getelementsbytagname() 不工作
VBA Excel MSXML2.XMLHTTP getelementsbytagname() not working
这是我的代码
Sub loadrss()
Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, topic As HTMLHtmlElement, i As Integer
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", "http://antt.vn/rss/trang-chu.rss", False
http.send
html.body.innerHTML = http.responseText
Set topics = html.getElementsByTagName("Item")
i = 55
For Each topic In topics
Sheet7.Cells(i, 15).Value = topic.getElementsByTagName("title")(0).innerText
Sheet7.Cells(i, 16).Value = topic.getElementsByTagName("link")(0).innerText
Sheet7.Cells(i, 17).Value = topic.getElementsByTagName("pubDate")(0).innerText
i = i + 1
Next
End Sub
但它警告此行有错误
Sheet7.Cells(i, 15).Value = topic.getElementsByTagName("title")(0).innerText
不知道怎么解决,求助!!!
我认为您需要探索与 XML 相关的 DOM 模型。否则,您最终将不得不尝试按照 topics(0).OwnerDocument.DocumentElement.outerText
从 OwnerDocument
中提取信息
我是新手,所以愿意接受反馈,但以下是我会采取的路线。
@Vityata 这里有一个很好的节点选择示例:
这是一个让您开始提取 pubDates 的示例。
Sub testing()
Dim xmlhttp As XMLHTTP60
Set xmlhttp = New MSXML2.ServerXMLHTTP60
Dim objXML As MSXML2.DOMDocument60 'MSXML2.DOMDocument
Set objXML = New MSXML2.DOMDocument60
With xmlhttp
.Open "GET", "http://antt.vn/rss/trang-chu.rss", False
.Send
Set objXML = .responseXML
End With
Dim elemList As IXMLDOMNodeList
Dim elem As IXMLDOMNode
Set elemList = objXML.GetElementsByTagName("pubDate")
For Each elem In elemList
Debug.Print elem.nodeTypedValue
Next elem
End Sub
参考资料(排名不分先后 - 这是我为了掌握导航而快速浏览的内容):
试试下面的代码。它应该为您获取所有您想要的值。
Sub XML_Parsing_ano()
Dim http As New XMLHTTP60
Dim xmldoc As Object, post As Object
With http
.Open "GET", "http://antt.vn/rss/trang-chu.rss", False
.send
Set xmldoc = CreateObject("MSXML2.DOMDocument")
xmldoc.LoadXML .responseXML.XML
End With
For Each post In xmldoc.SelectNodes("//item")
r = r + 1: Cells(r, 1) = post.SelectNodes(".//title")(0).Text
Cells(r, 2) = post.SelectNodes(".//pubDate")(0).Text
Cells(r, 3) = post.SelectNodes(".//link")(0).Text
Next post
End Sub
或者,如果您想坚持 .getElementsByTagName()
,那么:
Sub XML_Parsing_ano()
Dim http As New XMLHTTP60
Dim xmldoc As Object, post As Object
With http
.Open "GET", "http://antt.vn/rss/trang-chu.rss", False
.send
Set xmldoc = CreateObject("MSXML2.DOMDocument")
xmldoc.LoadXML .responseXML.XML
End With
For Each post In xmldoc.getElementsByTagName("item")
r = r + 1: Cells(r, 1) = post.getElementsByTagName("title")(0).Text
Cells(r, 2) = post.getElementsByTagName("pubDate")(0).Text
Cells(r, 3) = post.getElementsByTagName("link")(0).Text
Next post
End Sub
要添加到库中的引用:
Microsoft XML, v6.0
这是我的代码
Sub loadrss()
Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, topic As HTMLHtmlElement, i As Integer
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", "http://antt.vn/rss/trang-chu.rss", False
http.send
html.body.innerHTML = http.responseText
Set topics = html.getElementsByTagName("Item")
i = 55
For Each topic In topics
Sheet7.Cells(i, 15).Value = topic.getElementsByTagName("title")(0).innerText
Sheet7.Cells(i, 16).Value = topic.getElementsByTagName("link")(0).innerText
Sheet7.Cells(i, 17).Value = topic.getElementsByTagName("pubDate")(0).innerText
i = i + 1
Next
End Sub
但它警告此行有错误
Sheet7.Cells(i, 15).Value = topic.getElementsByTagName("title")(0).innerText
不知道怎么解决,求助!!!
我认为您需要探索与 XML 相关的 DOM 模型。否则,您最终将不得不尝试按照 topics(0).OwnerDocument.DocumentElement.outerText
OwnerDocument
中提取信息
我是新手,所以愿意接受反馈,但以下是我会采取的路线。
@Vityata 这里有一个很好的节点选择示例:
这是一个让您开始提取 pubDates 的示例。
Sub testing()
Dim xmlhttp As XMLHTTP60
Set xmlhttp = New MSXML2.ServerXMLHTTP60
Dim objXML As MSXML2.DOMDocument60 'MSXML2.DOMDocument
Set objXML = New MSXML2.DOMDocument60
With xmlhttp
.Open "GET", "http://antt.vn/rss/trang-chu.rss", False
.Send
Set objXML = .responseXML
End With
Dim elemList As IXMLDOMNodeList
Dim elem As IXMLDOMNode
Set elemList = objXML.GetElementsByTagName("pubDate")
For Each elem In elemList
Debug.Print elem.nodeTypedValue
Next elem
End Sub
参考资料(排名不分先后 - 这是我为了掌握导航而快速浏览的内容):
试试下面的代码。它应该为您获取所有您想要的值。
Sub XML_Parsing_ano()
Dim http As New XMLHTTP60
Dim xmldoc As Object, post As Object
With http
.Open "GET", "http://antt.vn/rss/trang-chu.rss", False
.send
Set xmldoc = CreateObject("MSXML2.DOMDocument")
xmldoc.LoadXML .responseXML.XML
End With
For Each post In xmldoc.SelectNodes("//item")
r = r + 1: Cells(r, 1) = post.SelectNodes(".//title")(0).Text
Cells(r, 2) = post.SelectNodes(".//pubDate")(0).Text
Cells(r, 3) = post.SelectNodes(".//link")(0).Text
Next post
End Sub
或者,如果您想坚持 .getElementsByTagName()
,那么:
Sub XML_Parsing_ano()
Dim http As New XMLHTTP60
Dim xmldoc As Object, post As Object
With http
.Open "GET", "http://antt.vn/rss/trang-chu.rss", False
.send
Set xmldoc = CreateObject("MSXML2.DOMDocument")
xmldoc.LoadXML .responseXML.XML
End With
For Each post In xmldoc.getElementsByTagName("item")
r = r + 1: Cells(r, 1) = post.getElementsByTagName("title")(0).Text
Cells(r, 2) = post.getElementsByTagName("pubDate")(0).Text
Cells(r, 3) = post.getElementsByTagName("link")(0).Text
Next post
End Sub
要添加到库中的引用:
Microsoft XML, v6.0