?
Solved

retrieve recursive information from xml file with .vbs script

Posted on 2014-08-12
2
Medium Priority
?
587 Views
Last Modified: 2014-08-12
Hi Experts,

I've got this script that does its job excellent in a situation where my XML has one parent child level.
But the XML (created by a program) sometimes creates a structure that nests multiple structures and this makes that my script does no longer work.

The structure has nodes called "folders" that contain one or multiple nodes "folder" but every folder can contain (optional) child nodes called "folders" and so on.

xml.JPG
So now I need to take it to a next level where it can retrieve the information recursive from the XML. And the main goal is to retrieve the information from the "document" nodes in combination of the parent folder ID and folder name

But how can I make this script recursive?

Set xmlDOM = CreateObject("MSXML2.DOMDocument")
xmlDOM.async = False
xmlDOM.Load "D:\OG\index.xml"
Set folder = XMLDom.SelectNodes("index/folders/folder")
waarden=""
i=0
Set db = CreateObject("ADODB.Connection")
CS = "Provider=SQLOLEDB;Data Source=mySource;Initial Catalog=myCatalog; User ID=myuser; Password=mypassword;"
db.open CS

For Each folderItem In folder
   folderid =folderItem.SelectSingleNode("id").text
   foldername=folderItem.SelectSingleNode("name").text
   
   		for each documentItem in folderItem.SelectNodes("documents/document")
			docid = documentItem.SelectSingleNode("id").text
			version = documentItem.SelectSingleNode("version").text
			title = documentItem.SelectSingleNode("title").text
			extension = documentItem.SelectSingleNode("extension").text
			summary = documentItem.SelectSingleNode("summary").text
			viewerurl = documentItem.SelectSingleNode("viewerurl").text
			Sql = "select * FROM externedocumenten"
			Set RecordSet = CreateObject("ADODB.Recordset")
			RecordSet.Open Sql, CS, 3, 3
			RecordSet.AddNew
			RecordSet.Fields("categorieID").Value = folderid
			RecordSet.Fields("categorienaam").Value = replace(foldername,"'", "")
			RecordSet.Fields("id").Value = docID
			RecordSet.Fields("versie").Value = version
			RecordSet.Fields("titel").Value = replace(title,"'", "")
			RecordSet.Fields("extentie").Value = "application/" & extension
			RecordSet.Fields("samenvatting").Value = replace(summary,"'", "")
			RecordSet.Fields("URL").Value = viewerurl
		
			Set mstream = CreateObject("ADODB.Stream")
			mstream.Type = 1
			mstream.Open
			mstream.LoadFromFile viewerurl
			RecordSet.Fields("bestand").Value = mstream.Read
			RecordSet.Update
			Set mstream = Nothing
			i=i+1
  		next
Next
i = 0
Set rs = Nothing 
Set xmlDOM = Nothing

Open in new window

0
Comment
Question by:Steynsk
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 40255447
Should be as easy as changing line 4 to:
Set folder = XMLDom.SelectNodes("//folders/folder")

Open in new window

Tested with this script:
Option Explicit

Dim msg, xmlDOM, folder, folderItem, folderid, foldername, documentItem, docid, title

msg = ""

Set xmlDOM = CreateObject("MSXML2.DOMDocument")
xmlDOM.LoadXml "<?xml version=""1.0"" encoding=""utf-8""?> <index> <exportdate>2014-01-01</exportdate> <folders> <folder> <id>1</id> <name>O&amp;G</name> <documents/> <folders> <folder> <id>2</id> <name>Beleid</name> <documents/> <folders> <folder> <id>3</id> <name>Algemeen</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>4</id> <name>x</name> <documents> <document> <id>4</id> <version>1.0</version> <title>doc4</title> <extension>.docx</extension> <summary/> <iconurl/> <viewerurl/> </document> </documents> <folders/> </folder> </folders> </folder> <folder> <id>5</id> <name>y</name> <documents/> <folders/> </folder> <folder> <id>6</id> <name>z</name> <documents/> <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
   
		msg = msg & "FLD " & folderid & " - " & foldername & vbcrlf

   		for each documentItem in folderItem.SelectNodes("documents/document")
			docid = documentItem.SelectSingleNode("id").text
			title = documentItem.SelectSingleNode("title").text

			msg = msg & "DOC " & docid & " - " & title & vbcrlf

  		next

Next
Set xmlDOM = Nothing

msgbox msg

Open in new window

this produces the following output:
FLD 1 - O&G
FLD 2 - Beleid
FLD 3 - Algemeen
DOC 1 - Directiebeoordeling O&G
DOC 2 - doc2
DOC 3 - doc3
FLD 4 - x
DOC 4 - doc4
FLD 5 - y
FLD 6 - z

Open in new window

0
 
LVL 1

Author Closing Comment

by:Steynsk
ID: 40255531
Hi Robert, You've helped me out BIG time.

Reuze bedankt.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question