Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: XML access the repeating node values

by haukex (Archbishop)
on Jun 26, 2018 at 09:55 UTC ( [id://1217427]=note: print w/replies, xml ) Need Help??


in reply to XML access the REPEATING NODE VALUES

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>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1217427]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (8)
As of 2024-04-18 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found