I want to contribute with this Perlita.
Some days ago I was faced with a system where users are created manually because it lacks a general user import facility. The system offers a specific user export for backup, portability or migration purposes. The output of the export is a XML file which is understood by an associated propietary import process. The XML output file is just a collection of user sections. Based on such a section, I created a XML template and applied on it variable substitution for specific fields using XML::Simple. In this way I was able to translate an external list of users into the required XML format for import. The resulting XML could be written to a file and then imported into the system saving a lot of time in the user creation. (In the code shown, I print the result trough an Emacs buffer, so no file writing code needed.)
Beside the magic simplification, connecting XMLout with XMLin was also an interesting data structure exercise using Data::Dumper.
use strict; require XML::Simple; my @usr_list = ( q{u00157:STAF:Mickey:Request:}, q{u14318:STAF:Sean:Request:}, q{u10044:ENF:Carol:Response:}, q{u11162:STAF:Sarah:User:}, q{u03763:ENF:Laura:Response:}, q{u12093:RES:Tim:Request:}, q{u12091:RES:Maria:Request:}, q{u14380:RES:Tom:Request:}, ); my ($s1, $s2, $s3, $s4); my $xs = XML::Simple->new(); print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<foo version=\"4.2\ +">\n"; foreach (@usr_list) { ($s1, $s2, $s3, $s4) = split ":"; print $xs->XMLout($xs->XMLin("userxml.tmp", ForceArray => 1, KeepRoot => 1, Variables => {s1 => $s1, s2 => $s2, s3 => $s3, s4 => $s4, } ), RootName => undef, ); } print "</foo>\n";
Here the XML template "userxml.tmp":
<?xml version="1.0" encoding="UTF-8"?> <user> <type>normal</type> <deleted>false</deleted> <name>${s1}</name> <user-attribute> <name>Description</name> <protected>false</protected> <value>${s2}</value> <group> <name>Users</name> </group> </user-attribute> <user-attribute> <name>E-mail</name> <protected>false</protected> <group> <name>Users</name> </group> </user-attribute> <user-attribute> <name>Name</name> <protected>false</protected> <value>${s3}</value> <group> <name>Users</name> </group> </user-attribute> <user-attribute> <name>User Given Name</name> <protected>false</protected> <value>${s3}</value> <group> <name>Users</name> </group> </user-attribute> <group-membership> <name>${s4}</name> </group-membership> <group-membership> <name>Users</name> </group-membership> </user>
Happy hacking!
|
|---|