Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

XML::Simple Usage

by AlexTape (Monk)
on Sep 19, 2012 at 12:13 UTC ( #994453=perlquestion: print w/replies, xml ) Need Help??

AlexTape has asked for the wisdom of the Perl Monks concerning the following question:

Omniscient Monks,

I try to use XML::Simple :-)
my $xml = XML::Simple->new(); my $xml_Load = $xml->XMLin("Input.xml"); print Dumper $xml_Load; $xml->XMLout($xml_Load,OutputFile => "Output.xml");
<opt> <global> <glob1 value="g1v" /> <glob2 value="g2v" /> <glob3 value="g3v" /> </global> <parmsets> <example> <local> <location value="location" /> <loc2 value="loc2v" /> <loc3 value="loc3v" /> </local> <regional> <region1 value="region1v" /> <region2 value="region2v" /> <region3 value="region3v" /> </regional> </example> </parmsets> </opt>
<opt> <global name="glob1" value="g1v" /> <global name="glob2" value="g2v" /> <global name="glob3" value="g3v" /> <parmsets name="example"> <local name="loc2" value="loc2v" /> <local name="loc3" value="loc3v" /> <local name="location" value="location" /> <regional name="region1" value="region1v" /> <regional name="region2" value="region2v" /> <regional name="region3" value="region3v" /> </parmsets> </opt>
im not able to force xml::simple to keep the structure of the input file..
even if i use the output structure i get really big troubles if there is only one note in <parmsets>
because xmlin did not mention it as a hash anymore..

anybody got an leading idea ?

$perlig =~ s/pec/cep/g if 'errors expected';

Replies are listed 'Best First'.
Re: XML::Simple Usage
by toolic (Bishop) on Sep 19, 2012 at 12:20 UTC
    XML::Simple ForceArray may do what you need:
    use warnings; use strict; use XML::Simple; my $xml = XML::Simple->new(ForceArray => 1); my $xml_Load = $xml->XMLin("Input.xml"); $xml->XMLout($xml_Load,OutputFile => "Output.xml"); # Here is the Output.xml file: __END__ <opt> <global> <glob1 value="g1v" /> <glob2 value="g2v" /> <glob3 value="g3v" /> </global> <parmsets> <example> <local> <loc2 value="loc2v" /> <loc3 value="loc3v" /> <location value="location" /> </local> <regional> <region1 value="region1v" /> <region2 value="region2v" /> <region3 value="region3v" /> </regional> </example> </parmsets> </opt>
Re: XML::Simple Usage
by Jenda (Abbot) on Sep 19, 2012 at 13:12 UTC

    See Simpler than XML::Simple. It's not trivial to set XML::Simple so that the data structure produced by XMLin is consistent and even harder to set it up so that the XMLout produces something resembling the original XML.

    If you need to make changes to a XML and do not plan to read the result again with XML::Simple, consider using a different tool.

    Enoch was right!
    Enjoy the last years of Rome.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://994453]
Approved by marto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2022-09-28 22:06 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (125 votes). Check out past polls.