视觉基础中的domdocument问题

时间:2012-08-03 07:51:28

标签: xml visual-studio-2010 vba domdocument ixmldomdocument

我正在考虑在visual basic中处理xml文件。当我尝试使用DOMDOCUMENT时,visual basic显示错误。它没有认识到什么是domdocument。我是否必须调用任何库才能使其正常工作。看看我的代码

Attribute VB_Name = "Module1"
Function getDistance(Origin As String, Destination As String) As Double
    Dim HttpReq As Object
    Dim myDomDoc As DOMDocument60
    Dim distanceNode As IXMLDOMNode
    Set HttpReq = CreateObject("MSXML2.XMLHTTP")
    HttpReq.Open "GET", "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" & Origin & "&destinations=" & Destination & "&sensor=false", False
    HttpReq.send
    MsgBox HttpReq.responseText
    Set myDomDoc = New DOMDocument60
    myDomDoc.LoadXML HttpReq.responseText
    Set distanceNode = myDomDoc.SelectSingleNode("/DistanceMatrixResponse/row/element/distance/value")
    getDistance = distanceNode.Text / 1000
End Function

1 个答案:

答案 0 :(得分:0)

项目参考:

System.XML

Imports System.Xml添加到您的导入列表

创建一个类似于:

的变量
Dim MyXMLDoc As New XmlDocument

要加载文档,请使用:

 MyXMLDoc.Load("Document path here")

要读取与此类似的节点应该有效:

Dim Items as Xml.XmlNodeList = MyXMLDoc.SelectNodes("ParentNode/ChildNodes")
Dim Item as Xml.XmlNode
Try
  For Each Item in Items
    Dim randomstring As String = Item.GetAttributes.GetNamedItem("ItemName").value
  Next
Catch ex As Exception
End Try
等等......

希望这是有用的