Andrej Kastrin am Montag, 3. Juli 2006 21:08: > Dear Perl community Hi Andrej
> I have to parse ID, TITLE and SYMBOL fields from the file below. In this > manner I wrote (modify actually) simple script to do that for me. > > <?xml version='1.0'?> > <entries> > <entry> > <id>001</id> > <title>FIRST TITLE</title> > <symbol>SYMBOL AAA</symbol> > <symbol>SECOND CCC</symbol> > </entry> > <entry> > <id>002</id> > <title>SECOND TITLE</title> > <symbol>SYMBOL HHH</symbol> > </entry> > </entries> > > Script below work well, if there is only one <symbol> field in each > entry. I have some troubles to implement second foreach structure to > loop through each symbol field and print it. > > Thanks in advance for any help, > ------------------------------------- > #!/usr/bin/perl use strict; use warnings; # and declare your variables with my,... > # use module > use XML::Simple; > # create object > $xml = new XML::Simple (KeyAttr=>[]); # ...for example here: my $xml = new XML::Simple (KeyAttr=>[]); > # read XML file > $data = $xml->XMLin("example.xml"); > # access XML data > foreach $e (@{$data->{entry}}){ > $id=$e->{id}; > $title=$e->{title}; > $symbol=$e->{symbol}; > print "$id|$title|$symbol\n"; > } The script generates following output: 001|FIRST TITLE|ARRAY(0x84ee214) 002|SECOND TITLE|SYMBOL HHH The 'ARRAY(0x84ee214)' part indicates that in the $symbol variable you're printing out is not a string, but an arrayref - containing multiple symbol entries. (you can check that, and inspect any nested data structure, with the Data::Dumper module). So, $symbol can contain a scalar (string) or an arrayref containing (string) scalars, depending of the number of symbol tags in the xml-document. You get what you want by modifying the print line, for example: print "$id|$title|", ref($symbol) # or (ref($symbol) eq 'ARRAY') ? join ',', @$symbol # arrayref in $symbol : $symbol, # scalar in $symbol "\n"; You may need another format for multiple value entries than a comma separated list. Hm, or do you need to repeat the parent tag path in such a case? Like: 001|FIRST TITLE|SYMBOL AAA 001|FIRST TITLE|SECOND CCC Then forget my modified print statement :-) (Your question is not clear to me in that respect) Hope this helps, Dani -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>