Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Finding node with attribute XML::LibXML

by codiac (Beadle)
on Oct 01, 2014 at 01:25 UTC ( #1102501=note: print w/replies, xml ) Need Help??


in reply to Finding node with attribute XML::LibXML

This is not an LibXML solution, but I thought I'd paste it in cases anyone prefers using more perlish syntax for munging XML.
use strict; use warnings; use XML::TreeBuilder; my $doc = XML::TreeBuilder->new(); $doc->parse_file("minimal.xml"); my @vol = $doc->look_down(_tag => 'Volume', VolumeCategory => 'L'); # also tried various quoting scheme... my $tmp = scalar(@vol); print "Number of entries: $tmp \n"; # 1

Replies are listed 'Best First'.
Re^2: Finding node with attribute XML::Twig
by Discipulus (Canon) on Oct 01, 2014 at 08:03 UTC
    ..and this a XML::Twig solution.. ;=)
    use warnings; use strict; use XML::Twig; my $xml=<<'XML'; <root> <ProductKey>99</ProductKey> <Volume VolumeCategory="L" MeasurementCategory="Real">0.063</Volume> <Volume VolumeCategory="L" MeasurementCategory="Real">0.098989</Volume +> <Volume VolumeCategory="cuft" MeasurementCategory="Real">2.2</Volume> </root> XML my @vol_cat_L; my $twig= new XML::Twig( twig_handlers => { '/root/Volume[@VolumeCate +gory="L"]' => sub {push @vol_cat_L, $_}, }, ); $twig->parse( $xml); print "Number of L volume category: ",scalar @vol_cat_L,"\n"; ##OUTPUT (note the line in example data added by me): Number of L volume category: 2

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2023-09-26 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?