将XML从HttpRequest转储到文本文件中

时间:2016-10-07 10:04:50

标签: xml vba httprequest xml-documentation

我从Ebay请求一些XML,但返回的XML对于VBA调试窗口来说太长了,因此我需要将返回的XML转储到文件中 - 作为n00b&在黑暗中抓住,我可以帮助我使用正确的语法来获取XMLHttpRequest.responseXML.XML&的内容。把它放到一个文件中。这是我的代码(一切都有效 - 我只需要写一行文件)

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Set oFile = fso.CreateTextFile("Z:\AccessData\Temp.xml")     

XMLHttpRequest.send (body)
objxmldoc.loadXML (XMLHttpRequest.responseXML.XML)
Debug.Print XMLHttpRequest.responseXML.XML ' valid XML showing at this point

2 个答案:

答案 0 :(得分:3)

您是否打算将数据保存到文件中。然后这样做。将XML作为字符串传递给文件。

Sub your_function()

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Set oFile = fso.CreateTextFile("Z:\AccessData\Temp.xml") 

Dim sResult as String
XMLHttpRequest.send (body)
sResult = XMLHttpRequest.responseText()
SaveToFile "d:\AccessData\Temp.xml", sResult

End Sub

Sub SaveToFile(sFileName, sContent)
    'Saves a string to a file and closes the file
    ' sample usage: SaveToFile "d:\test.txt", "test"
    Dim fs As Object
    Dim file As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set file = fs.CreateTextFile(sFileName, True)
    file.WriteLine (sContent)
    file.Close
End Sub

答案 1 :(得分:0)

您不必解析objxmldoc.loadXML (XMLHttpRequest.responseXML.XML),因为XMLHttpRequest.responseXML已经是DOM文档,当然如果您需要将其保存到文件中,只需调用save方法即可,如在XMLHttpRequest.responseXML.save("result.xml")中(我不是真正的VBA人,所以目前还不确定XMLHttpRequest.responseXML.save "result.xml"是否是正确的语法,但调用save方法就是你所需要的。)