Login | |
|
 |
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 >
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
|
|