Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

XML access the REPEATING NODE VALUES

by Magnolia25 (Sexton)
on Jun 26, 2018 at 02:57 UTC ( #1217418=perlquestion: print w/replies, xml ) Need Help??

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 (Bishop) 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2020-07-10 00:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?