我认为我个人的方法是创建一个 XML 对象,从查询的结果数组创建子节点,将子节点附加到父节点,然后将 .xml 从对象转储到文本文件中。
我很久以前写了以下通用函数,并认为它可能对你正在尝试做的事情有所帮助:
Function RequestAddChildNodeWithAttributes(NodeLocation, NodeName, NodeIndex, NodeAttributes, NodeValue)
Set parentNode = objXML.SelectNodes(NodeLocation).Item(NodeIndex)
Set newNode = objXML.CreateElement(NodeName)
For AttributeLoop = 0 To CInt(UBound(NodeAttributes) / 2) Step 2
If NodeAttributes(AttributeLoop) <> "" Then newNode.setAttribute NodeAttributes(AttributeLoop), NodeAttributes(AttributeLoop + 1)
Next
If NodeValue <> "" Then newNode.Text = NodeValue
parentNode.appendChild newNode
End Function
尝试运行以下 sn-p 以了解其工作原理:
Set objXML = CreateObject("Microsoft.XMLDOM")
objXML.loadXML("<parent/>")
RequestAddChildNodeWithAttributes "/parent", "row1", 0, Array("header1", "result1", "header2", "result2", "header3", "result3"), "nodeValue1"
RequestAddChildNodeWithAttributes "/parent", "row2", 0, Array("header1", "result1", "header2", "result2", "header3", "result3"), "nodeValue2"
Print objXML.xml
注意:您不需要在 NodeValue 参数中添加任何内容,因此只需传入一个空字符串值,它不会写入值但会填充属性。
您只需要从查询返回的数组中获取标题(数组中的位置 0),然后遍历其余行*。生成后,您可以使用以下代码创建结果文件并将 .xml 转储到其中:
Set FileOut = CreateObject("Scripting.FileSystemObject").CreateTextFile(ResultFileName, true)
FileOut.Write objXML.xml
FileOut.Close
ETA:如果您需要,我可以尝试挖掘代码以解析查询生成的数组。