Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: XML Parsing -- XML::Twig

by Discipulus (Canon)
on May 28, 2016 at 18:45 UTC ( [id://1164406]=note: print w/replies, xml ) Need Help??


in reply to XML Parsing

UPDATE: do not use XML::Simple; use XML::Twig see also Re: Parsing xml

use strict; use warnings; use XML::Twig; my %ok_cities; my $twig= XML::Twig->new(pretty_print => 'indented', twig_handlers=>{ '/cities/City/StreetList/Street'=>sub{ if ($_[1]->att('bars')!~/none/){ push @{$ok_cities{$_[1]->parent()->parent()->at +t('name')}}, $_[1]->att('name'); } } } ); $/=''; $twig->parse(<DATA>); print map {"In '$_' these streets have bars: ". (join',',@{$ok_cities{$_}}). "\n"} sort keys %ok_cities; __DATA__ <?xml version="1.0" encoding="UTF-8"?> <cities> <City name="Some City"> <Properties location="SomeCountry"/> <StreetList version="1"> <Street name="Foo Street" bars="none"/> <Street name="Bar Street" bars="plenty"/> </StreetList> </City> <City name="Other City"> <Properties location="Narnia"/> <StreetList version="5"> <Street name="Lovely Street" bars="some"/> <Street name="Gray Street" bars="none"/> </StreetList> </City> </cities>

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://1164406]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found