Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Reading b/w tags

by gzayzay (Sexton)
on Mar 20, 2006 at 22:10 UTC ( [id://538065]=perlquestion: print w/replies, xml ) Need Help??

gzayzay has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Few days a go, I asked about reading the content of a xml tag without the use of XML::Parser, and other modules. Today while fumbling around I wrote a code on how to read the content of specific tags and I thought I should share it.

open(DBASE, $path."database.txt") || die ("File does not exist, $!\n") +; while(<DBASE>) { chomp; print (LIST "\nTIME: ".localtime(time)."\n"); print (LIST "$_\n\n"); open (XFILE, $_) || die ("Can\'t open[$_], $!\n"); while(<XFILE>) { chomp; if($_ =~ /\<name\>/../\<\/name\>/) { print (LIST "$_\n"); } elsif($_ =~ /\<keywords\>/../\<\/keywords\>/) { print (LIST "$_\n"); } elsif($_ =~ /\<description\>/../\<\/description\>/) { print (LIST "$_\n"); } elsif($_ =~ /\<results\>/../\<\/results\>/) { print (LIST "$_\n"); } }close XFILE; } close LIST; close DBASE;

The above code is working find but if any monks could help me make it smarter, I will really appreciate it.

Thanks,

Edman

Replies are listed 'Best First'.
Re: Reading b/w tags
by mickeyn (Priest) on Mar 21, 2006 at 07:46 UTC
    hi gzayzay,

    try to replace your 'while' loop with this one:

    while (<XFILE>) { chomp; print (LIST "$_","\n") if /\<(name|keywords|description|results)\>/ .. /\<\/$1\>/; }

    Enjoy,
    Mickey

      Thanks a lot Mickey.

      Edman

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://538065]
Approved by wazoox
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 07:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found