Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Traverse xml tree

 
Logged in as: Guest
arrSession:exec spGetSession 2,16,31065
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Post a VBScript >> Traverse xml tree
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Traverse xml tree - 2/14/2006 4:20:25 AM   
  seanm


Posts: 15
Score: 0
Joined: 12/21/2005
From: Belfast, Ireland
Status: offline

'Basically this script can be used on any directory containing xml files.
'It was written for a static directory hierarchy in which only 2 folder names change.
'I have edited the file / directory names to help with reading
'A good place to look if stuck iwth xml tree traversal


Sub
buildName_Main
call initialise
WScript.Echo
"Completed..."

End
Sub 'End of main
'Call to main

buildName_Main


'**************************************
'INITIALISE VALUES AND CALL SUBS
'**************************************

Sub
initialise()
Dim Args
Set Args = WScript.Arguments
Dim logDate, lang, sourceFile, Output
'Error handling: Msg if no file to read from specified

If Args.Count = 2 Then

logDate = Args(0)
lang = Args(1)
else
WScript.Echo
"Need to specify the logs date and then the language"
WScript.Quit
End If


'Output file if sourceFile = Failedprocessed.xml

Set objFSOf = CreateObject("Scripting.FileSystemObject")
Set failed = objFSOf.CreateTextFile("C:\build\VB_SCRIPTS\Failed.txt")
'Output file if sourceFile = processed.xml

Set objFSOp = CreateObject("Scripting.FileSystemObject")
Set passed = objFSOp.CreateTextFile("C:\build\VB_SCRIPTS\Passed.txt")



strSrcFolder =
"\\<server>\<path>\...\...\" & logDate & "\" & lang & "\...\..."

Set srcFSO = CreateObject("Scripting.FileSystemObject")
Set srcFolder = srcFSO.Getfolder(strSrcFolder)
Set srcFiles = srcFolder.Files

'Create xml obj and load the sourceFile into it

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async =
false



for each file in srcFiles

if file.Name = "processed.xml" Then

sourceFile = strSrcFolder &
"\processed.xml"

xmlDoc.load(sourceFile)
Set Output = passed
elements xmlDoc, edbOutput
else if file.Name = "Failedprocessed.xml" Then

sourceFile = strSrcFolder &
"\Failedprocessed.xml"

xmlDoc.load(sourceFile)
Set Output = failed
elements xmlDoc, edbOutput
end if

end if

next



'call to elements Sub

'elements xmlDoc, Output



End
Sub ' End initialise

'**************************************
'ELEMENTS SUB FOR XML TREE TRAVERSAL
'**************************************

Sub
elements(XDoc, pfiles)
for each child in XDoc.childNodes

if (child.nodeType =1) Then ' determine that the node is an element

if (child.attributes.length > 0) Then 'If child has att. then the length() method returns 1

attributes child, pfiles
'call the attributes sub

end if
end if

if (child.hasChildNodes) Then

elements child, pfiles
'Recursive call


end if
next



end
Sub

'**************************************
'ATTRIBUTES SUB. PARSE ATTRIBUTE VALUES
'**************************************

Sub
attributes(node, pfiles)
for each att in node.attributes

if( att.name = "FileName") Then

pfiles.WriteLine att.Value
end if


next



End
Sub


< Message edited by seanm -- 2/14/2006 4:22:52 AM >
 
 
Revisions: 1 | Post #: 1
 
 RE: Traverse xml tree - 2/14/2006 9:51:14 AM   
  ebgreen


Posts: 5070
Score: 31
Joined: 7/12/2005
Status: online
Some comments:

1) You should use the code tags when posting code.
2) I assume it is the lack of code tags, but if not then you should practice block indentation. It makes code much easier to read. (When I did block indentation I found that you appear to have an End If statement with no matching If statement.)
3) Having so many linebreaks just makes linebreaks that make code easier to read less useful. Use whitespace sparingly and to define logical elements of the code.
4) THe code is well commented.
5) Use Option Explicit. It is well worth the effort IMO.
6) Use a consistent variable naming convention. It makes it easier to understand what is going on in the code.

These are all stylistic suggestions and here is the code with the suggestions implemented(except for the Option Explicit because the easiest way to fix that is to run the code and I don't feel like setting up directories to do that and without changing the variable names to have some consistency because I'm too lazy to do that too):

      

Some functional suggestions:

1) I realize that the use of a Main code block is required in some languages, but it is not required in vbScript and just adds lines of code and confusion without any corresponding benefit.
2) It is not necessary to have 3 FileSystemObjects. One would work just fine.
3) Unless I am missing something then this script will not read and write to output any Node Values, just attributes.

Here is the code with these changes (except #3 because I don't know if that is the intended behavior or not):

      

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to seanm)
 
 
Post #: 2
 
 RE: Traverse xml tree - 2/14/2006 10:35:41 AM   
  seanm


Posts: 15
Score: 0
Joined: 12/21/2005
From: Belfast, Ireland
Status: offline
 
First of all for the tips.

As you can probably see i'm a relative newbie to this.
Still an undergrad at college so i'm still on the learning curve.

I do indent my code (Programming 101 as the Americans say), but I don't know
how to use the code tags on this site (my 1st post here).

I am aware main is not required but I prefer to use anyway for consistency.
I was unaware that I could have used 1 fso, something to keep in mind.

The whole point on the script is to traverse an xml tree, find  the "FileName" attribtue
for each element and output that value in a txtFile.

Slainte a chara.
Sean

(in reply to ebgreen)
 
 
Post #: 3
 
 RE: Traverse xml tree - 2/14/2006 10:43:22 AM   
  ebgreen


Posts: 5070
Score: 31
Joined: 7/12/2005
Status: online
The script is a good example of recursion which is a very common question here. All of my comments were of course merely suggestions and should be taken as such. ]To use code tags, surround your code with [*code] and [*/code] (without the *) or click on the <% icon above.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to seanm)
 
 
Post #: 4
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Post a VBScript >> Traverse xml tree Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts