VBA Excel MSXML2.XMLHTTP getelementsbytagname()无效

时间:2018-02-08 04:45:23

标签: excel vba getelementsbytagname msxml2

这是我的代码

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

我不知道如何修复它,请帮忙!!!

2 个答案:

答案 0 :(得分:2)

尝试以下代码。它应该取出你所追求的所有价值。

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  

答案 1 :(得分:1)

我认为您需要探索与XML相关的DOM模型。否则,您最终将不得不尝试从OwnerDocument <{1}} topics(0).OwnerDocument.DocumentElement.outerText中提取信息。

我是新手,所以我愿意接受这方面的反馈,但以下是我要采取的路线。

@Vityata有一个很好的节点选择示例:Select a single node XML object using VBA

这是一个让您开始提取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

参考文献(没有特别的顺序 - 这是我快速了解导航手柄的方法):

  1. Combine two XMLnodelist in VBA
  2. XmlDocument.GetElementsByTagName Method (String)
  3. VBA Web Services XML .responseXML parse
  4. Node.ownerDocument
  5. How to parse XML using vba
  6. The HTML DOM Element Object
  7. XmlHttpRequest – Http requests in Excel VBA