Hi Monks,

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!


In reply to Just another use of XML::Simple by fishy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.