Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi jbrugger,

To explain the reasong why I am unconfortable with XML::Simple, let me show you an example:
#!/usr/bin/perl use XML::Simple; use Data::Dumper; my $xml1 =<<"eof"; <config logdir="/var/log/foo/" debugfile="/tmp/foo.debug"> <server name="sahara" osname="solaris" osversion="2.6"> <address></address> <address></address> </server> <server name="gobi" osname="irix" osversion="6.5"> <address></address> </server> <server name="kalahari" osname="linux" osversion="2.0.34"> <address></address> <address></address> </server> </config> eof my $xml2 =<<"eof"; <config logdir="/var/log/foo/" debugfile="/tmp/foo.debug"> <server attr="sahara" osname="solaris" osversion="2.6"> <address></address> <address></address> </server> <server attr="gobi" osname="irix" osversion="6.5"> <address></address> </server> <server attr="kalahari" osname="linux" osversion="2.0.34"> <address></address> <address></address> </server> </config> eof print "Structure1:\n".Dumper(XMLin($xml1,ForceArray => 1)); print "Structure2:\n".Dumper(XMLin($xml2,ForceArray => 1));

The two pieces of XML are identical exept that in the first one the attribute name (could also be id) is specified, in the second it is not. The result is a parse structure that is completely different, changing the first element from an array into a hash! This is with ForceArray turned on.
Structure1: $VAR1 = { 'debugfile' => '/tmp/foo.debug', 'server' => { 'kalahari' => { 'osversion' => '2.0.34', 'osname' => 'linux', 'address' => [ '', '' ] }, 'sahara' => { 'osversion' => '2.6', 'osname' => 'solaris', 'address' => [ '', '' ] }, 'gobi' => { 'osversion' => '6.5', 'osname' => 'irix', 'address' => [ '' ] } }, 'logdir' => '/var/log/foo/' }; Structure2: $VAR1 = { 'debugfile' => '/tmp/foo.debug', 'server' => [ { 'osversion' => '2.6', 'osname' => 'solaris', 'address' => [ '', '' ], 'attr' => 'sahara' }, { 'osversion' => '6.5', 'osname' => 'irix', 'address' => [ '' ], 'attr' => 'gobi' }, { 'osversion' => '2.0.34', 'osname' => 'linux', 'address' => [ '', '' ], 'attr' => 'kalahari' } ], 'logdir' => '/var/log/foo/' };

If you look at the semantics of things, why does changing an attribute name (whether it is 'name' or not) change the structure of the resulting parse tree? I find this illogical and prone to making mistakes while parsing an XML file, but maybe that's just me.

In reply to Re^5: XML Parse, Spanish Elements by misterb101
in thread XML Parse, Spanish Elements by senik148

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-12 15:01 GMT
Find Nodes?
    Voting Booth?

    No recent polls found