Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Perl XML Search

by scotb (Initiate)
on Jan 11, 2013 at 15:13 UTC ( #1012901=perlquestion: print w/replies, xml ) Need Help??
scotb has asked for the wisdom of the Perl Monks concerning the following question:

I need to provide a search option for the user where they search for a name and it returns the address of the user they entered.

$loopIndex = 0; $input = <STDIN>; chomp($input); foreach (@a) { if($_ =~ m/<name> [A-Z]/) { print "Contact: @a[$loopIndex -1]" ; } $loopIndex++; }

I am not sure what needs to be added to this or if it is even close! appreaciate your help

EDIT: The data is being pulled from an xml file. I am searching for the name but need to read back other elements such as address from the xml.

Replies are listed 'Best First'.
Re: Perl XML Search
by tobyink (Abbot) on Jan 11, 2013 at 15:37 UTC
    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'


      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
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      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.

Re: Perl XML Search
by sundialsvc4 (Abbot) on Jan 11, 2013 at 22:18 UTC

    If we are to extrapolate this toy-example into a requirement of any size (and, mind you, you might not), then I will hands-down suggest that you use a library that supports XSLT XPath ... and that you use this technique to retrieve results from your XML structure, instead of cobblestoned logic of your own.   CPAN has a plentitude of industrial-strength XML tools, they’re quite efficient, and I feel that they consistently deliver more maintainable and flexible solutions.   Build an XPath expression that describes what you want, and let its fingers do the walking for you.

      If it is to perform when the amount of data and number of requests grow to non-toy proportions, XPath is not going to be a usable solution. A real solution will import the data into a database, create a few indexes and query the database instead.

      Enoch was right!
      Enjoy the last years of Rome.

Re: Perl XML Search
by Anonymous Monk on Jan 11, 2013 at 15:19 UTC
    What data ?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012901]
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2018-07-21 18:02 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (449 votes). Check out past polls.