nikmit has asked for the wisdom of the Perl Monks concerning the following question:
Update:
Posting here seems to mobilise me to look harder, even on a Friday afternoon before a 3 day weekend...
XML::Simple seems to do what I asked for below.
Of course, comments still welcome.
------
I have been looking at XML parsing modules all day and either can't find the one I need or found it and couldn't figure out how to use it... so I humbly ask for help.
Given this XML
<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>
What is the best way to load only a single 'City' element with its related information, to query further?
I want to choose a street to visit while I'm in a given city with something like
my $current_city = 'Some City'; my $xml = Module->parse('myfile.xml', //City[@name=$current_city); my @street_list = $xml->findnodes('/Street'); print "In $current_city these streets have bars: "; foreach my $street (@street_list) { if ($street->{bars} ne 'none') { my $street_name = $street->{name}; print "$street_name"; } }
Running that would produce
In Some City these streets have bars: Bar Street
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML Parsing
by choroba (Cardinal) on May 27, 2016 at 14:04 UTC | |
|
Re: XML Parsing
by Laurent_R (Canon) on May 27, 2016 at 17:38 UTC | |
|
Re: XML Parsing -- XML::Twig
by Discipulus (Canon) on May 28, 2016 at 18:45 UTC |