以下是一些示例代码和一些有用的链接,它们可能会对您有所帮助。我强烈建议单步执行代码并随着代码的进行查看每个对象的状态。
此外,根据评论中的建议,您需要添加对 MSXML2 库的引用。在 VBA 开发窗口中,单击工具菜单和参考...(工具->参考)。出现引用对话框。向下滚动,直到找到 Microsoft XML, v6.0 选中复选框以添加它,单击确定按钮,您就可以参加比赛了。
一些有用的链接
WC3 Schools XML DOM Tutorial
Microsoft IXMLDOMText Object Members
Declare and set your URL
Dim myUrl As String: myUrl = "www.example.com/myfile.xml"
'Declare your xmlHTTP stream
'I prefer early binding rather than late binding
Dim xmlHTTP As MSXML2.ServerXMLHTTP60
xmlHTTP.Open "Get", myUrl, False
xmlHTTP.send
'Use reponseXML rather than responseText
'This way you get an XML DOM, rather than a string of text
'Declare XML DOM Document
Dim xmlDOMDoc As MSXML2.DOMDocument60
Set xmlDoc = xmlHTTP.responseXML
'Declare a rootNode as an XML DOM element
Dim rootNode As MSXML2.IXMLDOMElement
'Set the root node to the xmlDocumet (your HTTP stream)
'to the top document element
'In your case Root Node is data-set
Set rootNode = xmlDoc.DocumentElement
'Declare the Root Nodes children.
'In your case they are XML Element nodes
'with the name record
Dim xmlRootChildNode As MSXML2.IXMLDOMElement
'Your nodes of Root Children are Text Nodes
'in your example the names are Agency, Date and Code
Dim xmlChildrenOfRootChildNode As MSXML2.IXMLDOMElement
'Declare a string array to hold the text in your Text Nodes
Dim tnText(3) As String
Dim tnDictionary As Scripting.Dictionary
Dim nDx As Integer
'Loop through the Roots children
For Each xmlRootChildNode In rootNode.ChildNodes
'does the Root Child Node have children?
If xmlRootChildNode.HasChildNodes Then
nDx = 0
'This code will add them to the array
For Each xmlChildrenOfRootChildNode In xmlRootChildNode.ChildNodes
tnText(nDx) = xmlChildrenOfRootChildNode.text
Next
'Or if you want to use a Dictionary
For Each xmlChildrenOfRootChildNode In xmlRootChildNode.ChildNodes
'This adds a record to a Dictionary. It will contain
'The dictionary's key will be the nodeName aka tag (Agency, Date, Code)
'The dictionary's item will be the Text value stored between the xml tags
tnDictionary.Add xmlChildrenOfRootChildNode.nodeName, xmlChildrenOfRootChildNode.text
Next
End If
Next
将回车符 (CR)、换行符 (LF) 和制表符添加到 XLM 输出文件以便您可以阅读是一项挑战。我没有在网上找到任何真正有帮助的东西。下面的代码将根据需要插入 CRLF 和尽可能多的制表符。
用于指示方法在当前节点之后或之前添加空白的公共枚举:
'Public Enumerator used by the XMLAddSpace function
'This is an indicator telling the function
'where the CRLF and tabs are being added
Public Enum eAddBeforeAfter
After = 1
Before = 2
End Enum
XMLAddSpace 函数:
'*****************************************************************************************
'** **
'** Sub XmlAddSpace adds Carriage Return (CR), and Line Feed (LF) and as many tab **
'** characters specificed in tabCnt. It used vbCrLf for the CR and LF value and **
'** Chr(9) (ASCII Tab Character value 09) to set the ASCII tab character value. **
'** **
'** PARAMATERS: **
'** xmlNode as IXMLDOMElement Is the Node that the white space will be added **
'** after. **
'** tabCnt is the number of tab characters you want to indent the next line by **
'** BeforeAfter is an enum that directs the method to either add the white **
'** before the xmlChildNode or after the xmlNode **
'** xmlChildNode is optional when selecting After but required when selecting **
'** Before for adding white space before a node. White space is always **
'** before a child node element **
'*****************************************************************************************
Public Sub XmlAddSpace(ByRef xmlNode As MSXML2.IXMLDOMElement, ByVal tabCnt As Integer, _
ByVal BeforeAfter As eAddBeforeAfter, Optional ByRef xmlChildNode As MSXML2.IXMLDOMElement)
'Declare the text node that will hold the white space text
Dim nodeSpace As IXMLDOMText
'Declare a variable to hold the white space text
'We'll add the tab characters in the next few statements
'Start by putting CRLF as the front of the text string
Dim tabSpace As String: tabSpace = vbCrLf
'Now add the tab character to the string after CRLF
'this way the XML output has a new line follwed by 0 to n
'number of tab characters causing it to indent
If tabCnt > 0 Then
Dim i As Integer
For i = 1 To tabCnt
tabSpace = tabSpace & Chr(9)
Next
End If
'Now add the white space to the text node.
If BeforeAfter = After Then
'After puts white space after the current node
'This is useful for putting CRLF and indenting
'a parent node's closing tag
Set nodeSpace = xmlNode.OwnerDocument.createTextNode(tabSpace)
xmlNode.appendChild nodeSpace
Else
'Before puts white space before the current node
'This is useful for putting CRLF and indenting
'a new child from either a parent node or a sibling node
xmlNode.InsertBefore xmlChildNode.OwnerDocument.createTextNode(tabSpace), xmlChildNode
xmlNode.appendChild xmlChildNode
End If
End Sub
要使用方法你需要调用它如下:
在前面添加:XmlAddSpace parentNode, 2, Before
之后添加:XmlAddSpace parentNode, 2, After, childNode
注意父节点和子节点都必须是 MSXML2.IXMLDOMElement 类型