Ibrows
Ibrows

Reputation: 31

How can I import XML with only specific tag to Access database?

I have a XML file like this:

<Example>
  <A>
    <a1>1</a1>
    <a2>2</a2>
  </A>
  <B>
    <b1>1</b1>
    <b2>2</b2>
    <b3>3</b3>
  </B>
  <B>
  .
  .
  .
  </B>
</Example>

When I use Application.ImportXML Me.XMLpath, acStructureAndData, it will import both table A and B. So how could I just import table B without A? (A could be 10+ different tables). Thanks for your kindly help!

Upvotes: 2

Views: 2906

Answers (1)

Ibrows
Ibrows

Reputation: 31

Thanks for the hint, the following is what I done...

Dim db As Database
Set db = CurrentDb()
Dim xDoc As MSXML.DOMDocument
Set xDoc = New MSXML.DOMDocument
Dim xNote As IXMLDOMNode
Dim x As IXMLDOMNodeList
Dim XMLpath, sSQL As String

XMLpath = Me.Text1
xDoc.validateOnParse = False
sSQL = "INSERT INTO Temp VALUES ('"

If xDoc.Load(XMLpath) Then
    Set x = xDoc.getElementsByTagName("_TargetTag")
Else
    MsgBox "XML file ERROR"
    Exit Sub
End If

For j = 0 To x.length - 1
    For i = 0 To lengthOfChildNode
        sSQL = sSQL & x(j).childNodes(i).nodeTypedValue & "','"
    Next i
    sSQL = Left(sSQL, Len(sSQL) - 2) & ");"
    db.Execute sSQL
    sSQL = "INSERT INTO Temp VALUES ('"
Next j

So far I got what I need, but any idea that can improve this code I would be very appreciate. Thanks!

Upvotes: 1

Related Questions