Printing a Tag content
gzayzay
created: 2006-04-03 15:57:35
Hi Monks,

I am trying to collect a group of data from text file base on a couple of checks. Example: I have a text file that searched multiple xml file and extracted the following tags.


  c:\data\cat.xml
  dog, cat, fish, bird


  c:\data\cow.xml
  dog, cat, fish, bird, cow, goat


  c:\data\snake.xml
  dog, cat, fish, bird, snake, orange

What I am trying to do is to prompt the user to enter the name of the animal to find. If the user enters snake for example, I want to return the entire content of .. inculding the MS_ tags. I have tried the following code but I am getting nowhere. Could any monk kindly help me with this.

print ("Enter the word to search >> ");
chomp ($word = );
my $listen = 0;
open(SEARCH, "<$kwords")
while ()
{
  chomp;
  @array = ();
  foreach($_ =~ /\/../\<\/MS_$listen\>/;
  if ($_ =~ /$word/i)
  {
    print("@array[0..$#array]\n");
  }
}

Thanks,

Edman

Re: Printing a Tag content
created: 2006-04-03 16:43:57

Use [mod://XML::Twig] or [mod::XML::TreeBuilder]. Life is not long enough to roll your own XMH/HTML/CSV parser. To get you started:

use strict;
use warnings;
use XML::Twig;
use Data::Dump::Streamer;

my $twig = XML::Twig->new();

$twig->parse (do {local $/; });

my @chunks = $twig->root ()->children();
my %lookup;

for my $chunk (@chunks) {
    my $words = $chunk->first_child ('words');
    my $text = $words->xml_text ();
    push @{$lookup{$_}}, $chunk for split /,\s+/, $text;
}

#do stuff with %lookup

__DATA__

    
      c:\data\cat.xml
      dog, cat, fish, bird
    
    
      c:\data\cow.xml
      dog, cat, fish, bird, cow, goat
    
    
      c:\data\snake.xml
      dog, cat, fish, bird, snake, orange
    

DWIM is Perl's answer to Gödel
Re^2: Printing a Tag content
created: 2006-04-03 16:49:03
I knew someone would come up with the big XML libraries ;-)
Re: Printing a Tag content
created: 2006-04-03 16:44:20
You could either use a giant regex or use split with as pattern.
The following code uses the split way.
Use brackets to catch the opening tags.
Stuff it in a %hash.
Grep out the elem you wanted.
Reassemble the record with map.
Print it and be happy.
#!/usr/bin/perl -w
print ("Enter the word to search >> ");
chomp ($word = );
$/=undef;
$_=<>;
my @arr = split /()/;
shift @arr; # 1st element stuff before , so shift away
my %hash = @arr;
print map {($_, $hash{$_})} grep { $hash{$_} =~ $word } keys %hash;
Re^2: Printing a Tag content
created: 2006-04-03 16:57:37
Thanks,

Is this code of yours to go into the while loop of mine?? if note, the file with the tags is in a .txt file and I will need to open it and read the various lines. So, how do go about doing this.

Edman

Re^3: Printing a Tag content
created: 2006-04-03 17:23:45
No sorry. I was describing a way to read the xml from STDIN. If you want it using your $kwords file then use following version.
open SEARCH, "<$kwords" or die "can't open $kwords $!\n";
{
  local $/=undef; 
  $_ = ;

  my @arr = split /()/;
  shift @arr; # 1st element stuff before , so shift away
  my %hash = @arr;
  print map {($_, $hash{$_})} grep { $hash{$_} =~ $word } keys %hash;
}
close SEARCH;
Re^4: Printing a Tag content
created: 2006-04-03 17:28:13
Thanks you. And Thanks to all other monks for their assistance
Re^2: Printing a Tag content
created: 2006-04-04 08:09:19
Okies there is one more for the TIMTOWTDI and the golf fans.
This is probably bad style but see yourself.
#!/usr/bin/perl -w
print ("Enter the word to search >> ");
chomp ($word = );
{
  local $/=undef;
  print grep { $_ =~ $word }  =~ m!(.*?)!gs;
}
__DATA__

  c:\data\cat.xml
  dog, cat, fish, bird


  c:\data\cow.xml
  dog, cat, fish, bird, cow, goat


  c:\data\snake.xml
  dog, cat, fish, bird, snake, orange

perlmonks.org content © perlmonks.org and Anonymous Monk, codeacrobat, GrandFather, gzayzay

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03