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

Hello having som problem with a foreach using items in a xml-file. The file looks like:
<exec> <tc id=001/> <tc id=002/> <tc id=003/> </exec>

Im reading the file as:
my $xmlfile = new XML::Simple (Outputfile => $conf, ForceArray => 1, RootName=>'exec'); my $xml = $xmlfile->XMLin($conf);

And trying to the foreach:
foreach my $tc (@{$xml->{tc}}) { print "$tc"; }

Could I please get some help with the foreach. I want to get the tc's to a array and use them in a foreach.

Replies are listed 'Best First'.
Re: xml::simple foreach
by Herkum (Parson) on Dec 07, 2007 at 12:18 UTC
    To get a better idea of what your data structure looks like you should use Data::Dump or Data::Dumper.
    my $xmlfile = new XML::Simple (Outputfile => $conf, ForceArray => 1, RootName=>'exec'); my $xml = $xmlfile->XMLin($conf); use Data::Dump qw(dump); print "XML => " . dump( $xml );

    This should be a good starting point to see what you are working with.

Re: xml::simple foreach
by KurtSchwind (Chaplain) on Dec 07, 2007 at 12:27 UTC

    You are missing one of the huge advantages of XML::Simple. The fact that it returns a hash.

    If you want to see what's really going on, try this.

    use Data::Dumper; print Dumper ($xml);

    This will show you exactly how it's stored based on the options you gave it. Always do this if you run into issues. It clears things up in a hurry.

    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
Re: xml::simple foreach
by toolic (Bishop) on Dec 07, 2007 at 14:21 UTC
    Firstly, I am confused by your Perl code. I have no experience using XML::Simple (but I have used XML::Parser), but it seems odd that you specify the output file as $conf in your constructor statement, then you use $conf as an input file in your call to XMLin.

    Secondly, when I try to read your XML code in using XMLin, I get the following error trace:

    Uncaught exception from user code: not well-formed (invalid token) at line 2, column 10, byte 17 at /usr/ +lib64/perl5/vendor_perl/5.8.0/x86_64-linux-thread-multi/XML/Parser.pm + line 185 XML::Parser::parse('XML::Parser=HASH(0x803ab0)','*XML::Simple: +:XML_FILE') called at /usr/lib/perl5/vendor_perl/5.8.0/XML/Simple.pm +line 334 XML::Simple::build_tree_xml_parser('XML::Simple=HASH(0x8227a0) +','foo.xml','foo.xml') called at /usr/lib/perl5/vendor_perl/5.8.0/XML +/Simple.pm line 277 XML::Simple::build_tree('XML::Simple=HASH(0x8227a0)','foo.xml' +,'foo.xml') called at /usr/lib/perl5/vendor_perl/5.8.0/XML/Simple.pm +line 220 XML::Simple::XMLin('XML::Simple=HASH(0x8227a0)','foo.xml') cal +led at ./655609.pl line 15

    Here is my hacked code:

    #!/usr/bin/env perl use warnings; use strict; use diagnostics; use XML::Simple; my $conf = 'out.xml'; # I made this up my $xmlfile = new XML::Simple ( Outputfile => $conf, ForceArray => 1, RootName=>'exec' ); #my $xml = $xmlfile->XMLin($conf); my $xml = $xmlfile->XMLin('foo.xml');
    However, if I add double-quotes around the attribute values (001, 002, 003), I can read the XML file in without errors.

    Please be more specific about what problem you are having. Are you getting any warning or error messages? If so, please post them here.