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

I need to access the REPEATING NODE VALUES under size tag and prepare the output may be as. I need to stick with the use of XML::Simple only for now.

ITEM NUMBER: QWZ5671 COLORS: Red Burgundy

Your expert advice would be appreciated!!!

#!/usr/bin/perl -w use strict; use XML::Simple; use Data::Dumper; my $xmlData; my $xmlReader = XML::Simple->new(); $xmlData = $xmlReader->XMLin(); # print Dumper( %{$xmlData} ); ## ACCESS REPEATING NODE VALUES - __DATA__ <product description="watches" product_image="watches.jpg"> <item_number>QWZ5671</item_number> <size> <color_swatch image="red_watches.jpg">Red</color_swatch> <color_swatch image="burgundy_watches.jpg">Burgundy</color +_swatch> </size> </product>

Replies are listed 'Best First'.
Re: XML access the repeating node values
by haukex (Archbishop) on Jun 26, 2018 at 09:55 UTC
    I need to stick with the use of XML::Simple only for now.

    Sorry, but XML::Simple is almost never a good choice. Its own documentation says:

    PLEASE DO NOT USE THIS MODULE IN NEW CODE. ... The use of this module in new code is strongly discouraged. ... The major problems with this module are the large number of options (some of which have unfortunate defaults) and the arbitrary ways in which these options interact - often producing unexpected results.

    Please consider XML::LibXML, XML::Twig, or XML::Rules instead. A XML::LibXML solution:

    use warnings; use strict; use XML::LibXML; my $doc = XML::LibXML->load_xml(IO=>*DATA); for my $product ($doc->findnodes('//product')) { my $item = $product->findvalue('./item_number'); my @colors = map {$_->textContent} $product->findnodes('.//color_swatch'); print "ITEM NUMBER: $item COLORS: @colors\n"; } __DATA__ <product description="watches" product_image="watches.jpg"> <item_number>QWZ5671</item_number> <size> <color_swatch image="red_watches.jpg">Red</color_swatch> <color_swatch image="burgundy_watches.jpg">Burgundy</color_swa +tch> </size> </product>
Re: XML access the REPEATING NODE VALUES
by NetWallah (Canon) on Jun 26, 2018 at 04:22 UTC
    #!/usr/bin/perl -w use strict; use XML::Simple; use Data::Dumper; my $xmlReader = XML::Simple->new(); my $xmlData = $xmlReader->XMLin(<<"__XML__" ); <product description="watches" product_image="watches.jpg"> <item_number>QWZ5671</item_number> <size> <color_swatch image="red_watches.jpg">Red</color_swatch> <color_swatch image="burgundy_watches.jpg">Burgundy</color +_swatch> </size> </product> __XML__ print Dumper(\$xmlData ); print "ITEM NUMBER: " , $xmlData->{item_number}, " COLORS:", map ( { $_->{content} ." "} @{ $xmlData->{size}->{color_swatch} +} ), "\n";
    OUTPUT
    $VAR1 = \{ 'item_number' => 'QWZ5671', 'description' => 'watches', 'product_image' => 'watches.jpg', 'size' => { 'color_swatch' => [ { 'content' => 'Red', 'image' => 'red_watches.jpg' }, { 'content' => 'Burgundy', 'image' => 'burgundy_watches +.jpg' } ] } }; ITEM NUMBER: QWZ5671 COLORS:Red Burgundy

                    Memory fault   --   brain fried

      <Thank you. It worked !!!

A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.