Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Practical suggestion for accessing configuration data stored in XML format

by mirod (Canon)
on Mar 18, 2003 at 16:14 UTC ( #244055=note: print w/ replies, xml ) Need Help??


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

What's wrong with XML::Simple?

  1. XMLin( "file") looks pretty concise to me. Even when using the OO mode it is still pretty short: my $config= XML::Simple->new( forcearray => 1);
  2. I don't know what you mean by Integrate-able with perl OO, you get a clean data structure, what else would you want?
  3. the forcearray option makes XML::Simple behave sanely I believe.
  4. .

BTW I think your XML structure is not quite optimal, you repeat the X and Y when I believe they should mark a container object (see below).

Finally I have added a simplify method to XML::Twig that should be more or less equivalent to XMLin. It is very much in alpha state at the moment, but if you grab the development version of XML::Twig from xmltwig.com you can try the code below.

So here is the result with XML::Simple and with XML::Twig, output using YAML, both for your XML and for an updated version:

#!/usr/bin/perl -w use XML::Simple; use XML::Twig; use YAML; $/="\n\n"; my $xml= <DATA>; print "Original Version:\n"; print "Loaded using XML::Simple: \n", Dump( XMLin( $xml, forcearray => 1, keyattr => [])); print "Loaded using XML::Twig: \n", Dump( XML::Twig->new->parse( $xml)->simplify( keyattr => [])); $xml=<DATA>; print "Original Version:\n"; print "Loaded using XML::Simple: \n", Dump( XMLin( $xml, forcearray => 1, keyattr => [ 'name', 'type' +])); print "Loaded using XML::Twig: \n", Dump( XML::Twig->new->parse( $xml)->simplify( keyattr => [ 'nam +e', 'type'])); __DATA__ <?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> <?xml version='1.0' standalone='yes'?> <config name="MyConfig"> <blah name="X"> <foo type="T1"> A </foo> <foo type="T2"> B </foo> </blah> <blah name="Y"> <foo type="T3"> C </foo> <foo type="T4"> D </foo> </blah> <l1 foo1="bar1"> <l2 foo2="bar2"> <l3> Sometext </l3> </l2> </l1> </config>


Comment on Re: Practical suggestion for accessing configuration data stored in XML format
Download Code
Re: Re: Practical suggestion for accessing configuration data stored in XML format
by fuzzycow (Sexton) on Mar 18, 2003 at 17:22 UTC

    I'm not going to preach that my idea is the only solution, and everybody should use it. I'm sure most people would prefer XML::Simple (which is a good module, but using it can be a bit annoying) or XML::Twig (which is great, and many thanks for it!).

    The replies that I'm getting for my post is exactly the feedback that I wanted to get - in order to see if there is a need for a module like mine.

    However, I do want to note that the problem I wanted to solve was not loading XML file, but quickly accessing the loaded data. With XML::Simple you have to constantly think about the actual data representation in memory (speaking from experience). And the moment you start using 'forcearray' all the initial easiness disappears. From that point on you have to assume that each element is wrapped by an anon. array. (IMHO) Person using module for reading configuration files should not need to think too much about underling data structures or XML.

    I just think that it should be both possible and easy to access parameters in the XML configuration file using just a single line of code. Afterall, it's perl we are talking about ;)

    P.S.: I gave that ugly example intentionally to demonstrate that using method access, you can get get ether just the first element or all of them. This is exactly what XML::Simple lacks.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://244055]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2014-07-25 01:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls