Below code can be used to convert directory structure in xml.
require ‘rubygems’
require ‘rexml/document’
class DirectoryStructure
@@excludes = ["…","."]
@@doc = REXML::Document.new %{ <?xml version="1.0" encoding="UTF-8" ?> }
def self.traverse_directory(root_path)
#traverse initial directory structure
folder = @@doc.add_element( ‘myroot’,{‘id’ => root_path} )
#call loop_path with root_path and parent_node
loop_path(root_path,folder)
#create xml file
open( “final_directory_structure.xml”, “w” ) do |f|
f.puts @@doc
end
end
def self.loop_path(path,parent_node)
dir_entries= Dir.entries(path)
don’t need ‘.’,’…’
dir_entries = dir_entries-[’.’,’…’]
dir_entries.each do | entry |
check if directory or file
if File.directory?(path+"/"+entry)
#Folders Loop -------------------------------------------------
#Gets the directories of the current node.
#It changes for each recursive call
folder = parent_node.add_element( ‘folder’ ,{‘label’ =>entry
,‘file_path’ => path+"/"+entry } )
loop_path( path+"/"+entry, folder )
else
#Files Loop ------------------------------------------------------
file = parent_node.add_element( ‘file’ ,{‘label’ =>entry ,‘file_path’
=> path+"/"+entry } )
end
end
end
#first call to traverse directory
self.traverse_directory(“Authentication/Authentication”)
end