[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

Retrieve parent - child relation from a XML file

Hi experts,

I've got this XML file that represents a nested parent-child relationship between folders.

I'd like to save these relationships in to my database but before I can save them I have to retrieve them. And I have little knowledge reading XML in VBS.

My table is simple it has these fields :

ID (record identifier)
parentID
folderID
folderName

I've got a XML structure like in the image beneath. As you can see I've simplified it caling the folders grandmother, parent1, child1,2:

xml.JPG
The desired result I like to be able to collect in my table should be something like:

table.JPG  

But the DB part is no problem and can be done by myself.
0
Steynsk
Asked:
Steynsk
1 Solution
 
Robert SchuttSoftware EngineerCommented:
Based on the script from your previous question http:/Q_28495636.html and assuming ID is automatically generated:
Option Explicit

Dim msg, xmlDOM, folder, folderItem, folderid, foldername, parentID

msg = ""

Set xmlDOM = CreateObject("MSXML2.DOMDocument")
xmlDOM.LoadXml "<?xml version=""1.0"" encoding=""utf-8""?> <index> <exportdate>2014-01-01</exportdate> <folders> <folder> <id>384</id> <name>Grandmother</name> <documents/> <folders> <folder> <id>404</id> <name>Parent1</name> <documents/> <folders> <folder> <id>405</id> <name>Child1.1</name> <documents> <document> <id>1</id> <version>1.0</version> <title>Directiebeoordeling O&amp;G</title> <extension>.docx</extension> <summary/> <iconurl/> <viewerurl/> </document> <document> <id>2</id> <version>1.0</version> <title>doc2</title> <extension>.docx</extension> <summary/> <iconurl/> <viewerurl/> </document> <document> <id>3</id> <version>1.0</version> <title>doc3</title> <extension>.docx</extension> <summary/> <iconurl/> <viewerurl/> </document> </documents> <folders/> </folder> <folder> <id>407</id> <name>Child1.2</name> <documents> <document> <id>4</id> <version>1.0</version> <title>doc4</title> <extension>.docx</extension> <summary/> <iconurl/> <viewerurl/> </document> </documents> <folders/> </folder> <folder> <id>408</id> <name>Child1.3</name> <documents/> <folders/> </folder> </folders> </folder> <folder> <id>389</id> <name>Parent2</name> <documents/> <folders> <folder> <id>385</id> <name>Child2.1</name> <documents/> <folders/> </folder> </folders> </folder> </folders> </folder> </folders> </index>"

Set folder = XMLDom.SelectNodes("//folders/folder")

For Each folderItem In folder
	folderid =folderItem.SelectSingleNode("id").text
	foldername=folderItem.SelectSingleNode("name").text

	If folderItem.parentNode.parentNode.nodeName = "folder" Then
		parentID = folderItem.parentNode.parentNode.selectSingleNode("id").Text
	Else
		parentID = 0 ' top level
	End If

	msg = msg & "INSERT INTO ... (parentID, folderID, folderName) VALUES (" & parentID & ", " & folderid & ", '" & Replace(foldername, "'", "''") & "')" & vbcrlf
Next
Set xmlDOM = Nothing

msgbox msg

Open in new window

0
 
SteynskAuthor Commented:
Again Robert. Thank you very much for excellent your help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now