Hello,
Given the following XML config file, is there a better way to parse it than
the method below?
Goal:
servers = {
“HQ” => {
“host” => “orbite.eurocontrol.be”,
“dn” => “uid=roberto”,
“pass” => “xxxxxxx”,
“base” => “o=eurocontrol”,
“filt” => “(eurocontrolsiteid=Bretigny)”,
“attributes” => [
“uid”,
“sn”,
“givenname”,
“departmentname”,
“eurocontrolroomid”,
“telephonenumber”,
“mail”,
“userpassword”,
],
},
“EEC” => {
“host” => “ldap.eurocontrol.fr”,
“dn” => “”,
“pass” => “xxxxx”,
“base” => “o=eurocontrol”,
“filt” => “(&(objectclass=inetOrgPerson)(objectclass=mailRecipient))”,
“attributes” => [
“uid”,
“sn”,
“givenname”,
“ou”,
“roomnumber”,
“telephonenumber”,
“mail”,
“userpassword”,
],
},
}
···
-=-=-
$debug = true
def parse_config(config_file)
begin
config = XmlConfigFile.new(config_file)
rescue => err
$stderr.puts "Error loading #{config_file}: #{err}\n"
end
server_list = Array.new
config.get_parameter_array("//servers/name").each {|h|
h.each_value {|v| server_list << v }
}
Now for every server, get all attributes
servers = Hash.new
server_list.each {|s|
#
# New server
#
puts “#{s}:” if $debug
servers[s] = Hash.new
# Get everything (attributes will be treated later)
#
config.get_parameter_array("//server[@name='#{s}']/*").each {|h|
h.each {|k,v|
attr = (k.split(/\./))[1]
servers[s][attr] = v
puts " #{attr} -> #{v}" if $debug
}
}
# Now get all attributes/* values
#
servers[s]["attributes"] = Array.new
config.get_parameter_array("//server[@name='#{s}']/attributes/*").each {|h|
h.each {|k,v|
attr = (k.split(/\./))[2]
servers[s]["attributes"] << v
puts " attributes -> #{v}" if $debug
}
}
}
return server_list
end
-=-=-
config.xml
-=-=-
<?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>
HQ
EEC
orbite.eurocontrol.be
uid=roberto
xxxxx
o=eurocontrol
(eurocontrolsiteid=Bretigny)
uid
sn
givenname
departmentname
eurocontrolroomid
telephonenumber
mail
userpassword
ldap.eurocontrol.fr
cn=Directory Manager
xxxxxx
o=eurocontrol
(objectclass=inetOrgPerson)
uid
sn
givenname
ou
roomnumber
telephonenumber
mail
userpassword
telephonenumber
telephonenumber
>attr.sub(%r{^.*?(\d{4})$}, ‘\1’)
eurocontrolroomid
roombumber
telephonenumber
telephonenumber
>"+33-1-5555" + attr
roombumber
eurocontrolroomid
-=-=-
Ollivier ROBERT -=- Eurocontrol EEC/ITM -=- roberto@eurocontrol.fr
Usenet Canal Historique FreeBSD: The Power to Serve!