Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. <rng1>
  2. <col1>
  3. <row1>A</row1>
  4. <row2>B</row2>
  5. <row3>C</row3>
  6. <row4>D</row4>
  7. <col2>
  8. <col1>
  9. <row1>E</row1>
  10. <row2>F</row2>
  11. <row3>G</row3>
  12. <row4>H</row4>
  13. <col2>
  14. </rng>
  15.  
  16. ' get a list of the col elements (thi sits in a loop to go through them all)
  17. Set oXMLColNodeList = oXMLDoc.selectNodes("//saveStates/rng/*")
  18.  
  19. ' Lop through each column
  20. For colNum = 1 To oXMLColNodeList.Length
  21. ' get all the row nodes for that coulmn
  22. Set oXMLRowNodeList = oXMLDoc.selectNodes("//saveStates/rng"/col" & colNum & "/*")
  23.  
  24. ' loop through all the row nodes
  25. For rowNum = 1 To oXMLRowNodeList.Length
  26. ' get the node to do something with it
  27. Set oXMLNode = oXMLDoc.selectSingleNode("//saveStates/rng/col" & colNum & "/row" & rowNum)
  28.  
  29.  
  30. next rowNum
  31. next colNum
  32.  
  33. Public Function RecordsetFromXMLDocument(XMLDOMDocument As DOMDocument) As Recordset
  34. Dim oRecordset As ADODB.Recordset
  35. dim destRange as range
  36.  
  37. set destrange=range("B2")
  38.  
  39. Set oRecordset = New ADODB.Recordset
  40.  
  41. oRecordset.Open XMLDOMDocument 'pass the DOM Document instance as the Source argument
  42.  
  43. Set RecordsetFromXMLDocument = oRecordset 'return the recordset
  44.  
  45. destRange.CopyFromRecordset oRecordset
  46.  
  47. Set oRecordset = Nothing
  48.  
  49. End Function
  50.  
  51. Option Explicit
  52. Sub ReadRows()
  53.  
  54. 'Declarations
  55. Dim oXMLDoc As Object ' XML document (object)
  56. 'Dim oXMLColNodeList As Object ' you don't need this list any more !
  57. Dim oXMLRowNodeList As Object ' needed node list
  58. Dim oXMLNode As Object ' single node within this list
  59. Dim xPE As Object ' ParseError object
  60. Dim strErrText As String ' Error message
  61. Dim sFileName As String ' user defined file path
  62. ' =================================
  63. ' 0. User defined File Name <== !
  64. ' =================================
  65. sFileName = ThisWorkbook.Path & "datatest.xml"
  66. ' =================================
  67. ' 1. XML Declaration oXMLDoc
  68. ' =================================
  69. ' Set oXMLDoc = New MSXML2.DOMDocument60 ' Early Binding
  70. Set oXMLDoc = CreateObject("Msxml2.DOMDocument.6.0") ' Late Binding
  71.  
  72. ' XPath, Validation
  73. oXMLDoc.setProperty "SelectionLanguage", "XPath"
  74. oXMLDoc.validateOnParse = False
  75. ' =================================
  76. ' 2. Load XML File
  77. ' =================================
  78. If oXMLDoc.Load(sFileName) Then
  79. MsgBox sFileName & " successfully loaded"
  80. Else
  81. ' Set ParseError Object
  82. Set xPE = oXMLDoc.parseError
  83.  
  84. With xPE
  85. strErrText = "Load Error " & .ErrorCode & " XML File " & vbCrLf & _
  86. Replace(.URL, "file:///", "") & vbCrLf & vbCrLf & _
  87. xPE.reason & _
  88. "Source Text: " & .srcText & vbCrLf & vbCrLf & _
  89. "Line No: " & .Line & vbCrLf & _
  90. "Line Pos: " & .linepos & vbCrLf & _
  91. "File Pos: " & .filepos & vbCrLf & vbCrLf
  92. End With
  93. MsgBox strErrText, vbExclamation
  94. Set xPE = Nothing
  95. Set oXMLDoc = Nothing
  96. 'Stop procedure
  97. Exit Sub
  98. End If
  99. ' =================================
  100. ' 3 Set only one Nodelist
  101. ' =================================
  102. Set oXMLRowNodeList = oXMLDoc.DocumentElement.SelectNodes("rng/*/*")
  103.  
  104. ' =================================
  105. ' 4. loop through all (row) nodes
  106. ' =================================
  107. For Each oXMLNode In oXMLRowNodeList
  108. Debug.Print oXMLNode.Text
  109. ' read in ...
  110. Next oXMLNode
  111.  
  112. ' 5. terminate oXMLDoc
  113. Set oXMLDoc = Nothing
  114. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement