如何从Microsoft Access中的IXMLDOMNodeList获取原始xml?

时间:2009-09-10 22:59:14

标签: xml ms-access

如何从MSXML2中提取原始Xml文本 - Microsoft Access中的IXMLDOMNodeList?

4 个答案:

答案 0 :(得分:1)

您可以在每个IXmlDomNode上使用Xml property

答案 1 :(得分: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

答案 2 :(得分: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

这是动态写的。在离开过程之前,请检查要关闭的对象/设置为空。

答案 3 :(得分:0)

这实际上取决于你打算用xml做什么。在Access 2003和2007中,我们可以导入xml。

因此,如果您有一个xml字符串是一个数据表,那么您可以接收数据,写入磁盘,然后执行Application.ImportXML。这种方法比其他答案更少的代码。

这是一个代码剪辑这个概念:

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>