Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
We don't bite newbies here... much
 
PerlMonks  

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");
Input.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>
Output.xml
<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';

Comment on XML::Simple Usage
Select or Download Code
Re: XML::Simple Usage
by toolic (Chancellor) 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.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (16)
As of 2014-04-23 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (549 votes), past polls