Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

    Excellent!

    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?

    Thanks.

    Jim

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2015-07-28 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (251 votes), past polls