Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Perl XML Search

by tobyink (Abbot)
on Jan 11, 2013 at 15:37 UTC ( #1012905=note: print w/ replies, xml ) Need Help??

in reply to Perl XML Search

use 5.012; use strict; use warnings; use Ask qw(:all); use XML::LibXML 2; my $xml = XML::LibXML->load_xml(IO => \*DATA); my $who = entry("Who are you looking for?"); my ($person) = $xml->findnodes(qq{//person[name/text() = "$who"]}) or error("Cannot find '$who'\n") and die; my @lines = $person->findnodes(qq{adr/*})->map(sub { $_->textContent } +); info(join "; ", @lines); __DATA__ <contacts> <person> <name>Barack Obama</name> <adr> <street-address>1600 Pennsylvania Avenue Northwest</street +-address> <locality>Washington</locality> <region>DC</region> <postal-code>20500</postal-code> <country>United States</country> </adr> </person> <person> <name>David Cameron</name> <adr> <street-address>10 Downing Street</street-address> <locality>London</locality> <postal-code>SW1A 2AA</postal-code> <country>United Kingdom</country> </adr> </person> </contacts>
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Comment on Re: Perl XML Search
Download Code
Replies are listed 'Best First'.
Re^2: Perl XML Search
by Jim (Curate) on Jan 11, 2013 at 17:10 UTC


    What does the 2 in use XML::LibXML 2; do? Where is it documented? I can't find it in the documentation. Also, what's the best tutorial to use to learn XML::LibXML?



      It's documented in use:
      use Module VERSION
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: Perl XML Search
by topher (Scribe) on Jan 16, 2013 at 06:12 UTC

    This is the correct approach to go about solving this problem. For almost any case where you're dealing with XML and you aren't writing a throw-away one-liner, you should be using an XML module/library to do the work for you. It will be easier, cleaner, and much more likely to work.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2015-12-01 23:27 GMT
Find Nodes?
    Voting Booth?

    My keyboard shows this many letters:

    Results (29 votes), past polls