2009-09-10 67 views

回答

1

(此查詢是由代理髮布給我的。)所需的確切知識是採取由Web服務api返回的MSXML2.IXMLDOMNodeList對象,並從節點中提取數據。我有一段工作代碼可以用於XML版本,但不希望成爲MSXML專家,只是爲了讓一個函數可以工作。以下是使用XML輸入的版本,如何將其更改爲使用nodeList輸入? (我也可以輸出節點列表爲XML嗎?這將有助於剩下的步驟。

(增加:) 我剛剛試過帶節點的.xml屬性,實際上給了我整個XML。顯然,api用單個節點發送一個節點列表,然後將.xml屬性應用於該單個節點會生成一個完整的XML文檔(我無法弄清楚如何在這裏顯示,XML是在預覽中處理的) (/添加)

 
Private Sub ParseXML_Click() 

Dim thisXML As MSXML2.DOMDocument 
Dim fSuccess As Boolean 
Dim oRoot  As MSXML2.IXMLDOMNode 
Dim oBooking As MSXML2.IXMLDOMNode 
Dim oChildren As MSXML2.IXMLDOMNodeList 
Dim oChild As MSXML2.IXMLDOMNode 
Dim theDB As Database 
Dim theRST As Recordset 

Set theDB = CurrentDb 
Set theRST = theDB.OpenRecordset("XMLBookings", dbOpenDynaset) 

Set thisXML = New MSXML2.DOMDocument 
thisXML.async = False 
thisXML.validateOnParse = False 
fSuccess = thisXML.LoadXml(Bookings_fld) 
' If anything went wrong, quit now. 
If NotfSuccess Then 
    GoTo ExitHere 
End If 

Set oRoot = thisXML.documentElement 
For Each oBooking In oRoot.childNodes 
    theRST.AddNew 
    Set oChildren = oBooking.childNodes 
    For Each oChild In oChildren 
     Select Case oChild.nodeName 
     Case "EventName" 
'   MsgBox "EventName = " & oChild.nodeTypedValue 
      theRST!EventName = oChild.nodeTypedValue 
     Case 
     (etc) 

     End Select 
    Next oChild 
     theRST.Update       ' Save changes. 
Next oBooking 

MsgBox "Done!" 
theRST.Close 
Set theDB = Nothing 

ExitHere: 
     Exit Sub 
HandleErr: 
     MsgBox "Error " & Err.Number & ": " & Err.Description 
     Resume ExitHere 
     Resume 

End Sub 
0

你會用VBA做到這一點。

相應的MSXML DLL添加到您的工具

編寫一些代碼,像這樣的:

Public Function getFromXML(x_fileXML as string) as string 

On Error GoTo ERREUR 

     Dim doc_xml As MSXML2.DOMDocument, _ 
      list_xml As IXMLDOMNodeList, _ 
      i As Long, _ 
      m_getFromXML as string 


Set doc_xml = New MSXML2.DOMDocument 

doc_xml.Load x_fileXML 

'i am interested by the nodeLists having a "attributeType: tag"' 
Set list_xml = doc_xml.getElementsByTagName("s:AttributeType") 
For i = 0 To (list_xml.Length - 1) 
    'extract the requested data here' 
    'something like m_getFromXML = m_getFromXML & ";" & ...' 
Next i 

getFromXML = m_getFromXML 

這是寫在飛。在離開proc之前,請檢查要關閉的對象/設置爲空。

0

這真的取決於你打算用XML做什麼。在獲得2003年和2007年,我們可以導入XML。

因此,如果您有一個xml字符串是數據表,那麼您可以接收數據,寫入磁盤,然後執行Application.ImportXML。這種方法是FAR較少代碼,然後在這裏給出的其他答案。

下面是一個代碼剪斷這一概念:

Dim intF  As Integer 
Dim strF  As String 
Dim objXML  As Object 

Set objXML = CreateObject("MSXML2.XMLHTTP") 
objXML.Open "GET", "URL string here", False 
objXML.Send 

strF = "c:\t.xml" 
intF = FreeFile 
Open strF For Output As intF 
Write #intF, objXML.responsetext 
Close 

Application.ImportXML "c:\t.xml", acAppendData 

上面將認爲XML有一個目錄,或在文本表DEF

例如:

<CATALOG> 
<tblContacts> 
<ID>1</ID> 
<Last_x0020_Name>Kallal</Last_x0020_Name> 
<First_x0020_Name>Albert</First_x0020_Name> 
</tblContacts> 
<tblContacts> 
<ID>2</ID> 
<Last_x0020_Name>Smo</Last_x0020_Name> 
<First_x0020_Name>Joe</First_x0020_Name> 
</tblContacts> 
</CATALOG> 
相關問題