Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re^2: XML Parse, Spanish Elements

by jbrugger (Parson)
on Feb 13, 2006 at 10:19 UTC ( #529782=note: print w/ replies, xml ) Need Help??

in reply to Re: XML Parse, Spanish Elements
in thread XML Parse, Spanish Elements

I'm not such a big fan of XML::Simple since the parsing into different types is very unconfortable. For example if a node contains one subnode, the subnode is a hash element in the node itself,

You can use use the argument ForeArray to overcome that problem:
my $xs = new XML::Simple(ForceArray => 1); Now all elements are read as an array.

"We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.

Comment on Re^2: XML Parse, Spanish Elements
Select or Download Code
Re^3: XML Parse, Spanish Elements
by misterb101 (Sexton) on Feb 13, 2006 at 11:11 UTC
    Hi jbrugger, Does that also mean that it will return an array of arrays of arrays....?
    The downside of something like that is that you would have to use $xml->[3]->[3]->[1]->[2] structures to reference nodes from your XML file. So you have to use you knowledge about the structure of the XML file to be able to reference nodes. That feels unconfortable to me at least.
    The nice thing about XML::XPath is that you can use full xpath expressions like /document//element_somwhere[@att="val"]/../next-sibbling or anything like that.

    before this starts sounding like a sales-pitch for XML::XPath, I think users should just use what feels most confortable.
      From the docs:
      ForceArray => 1 <opt> <name>value</name> </opt> would parse to this: { 'name' => [ 'value' ] } instead of this (the default): { 'name' => 'value' }
      "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
        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.

        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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-09-21 14:25 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (172 votes), past polls