During one of my recent perl programming projects I needed a way to store configuration for module instance creation. I chose XML as a format for configuration storage and stated looking for a suitable module for reading XML files.

Very soon I discovered there was no existing module that satisfied my requirements:

1. Very concise usage syntax

2. Integrate-able with perl OO

3. Sane treatment of multi-value results

I'll skip some history here, but let's just say that I've ended-up writing proof-of-concept perl module (XML::Twig wrapper) based on the idea of AUTOLOAD'ing methods based on element tag names. I'm well aware of the downsides of this approach, but I suggest you look at the examples below before you start 'flaming' ;)

I know you can get simular results with XPath, but that IMHO is not well suited for configuration parsing.

If anybody is interested I'll try to release the module source on CPAN.

################################################ # XML example: <?xml version='1.0' standalone='yes'?> <config name="MyConfig"> <blah name="X" type="T1"> A </blah> <blah name="X" type="T2"> B </blah> <blah name="Y" type="T3"> C </blah> <blah name="Y" type="T4"> D </blah> <l1 foo1="bar1"> <l2 foo2="bar2"> <l3> Sometext </l3> </l2> </l1> </config> ############################################3 # Module usage example: my $conf = Config::MagicXML::Base->new_from_xml('config.xml'); print $conf->blah('Y')->attr('type'),$/; # prints 'T3' print $conf->blah('Y')->pcdata(),$/; # prints ' C ' print map { $_->pcdata(),$/ } $conf->blah('X'); # prints # A # B print map { $_->attr('type'), "\t", $_->pcdata(), $/ } $conf->blah(); # prints: #T1 A #T2 B #T3 C #T4 D print $conf->l1()->l2()->l3->pcdata(),$/; #prints 'Sometext'

In reply to Practical suggestion for accessing configuration data stored in XML format by fuzzycow

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.