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

Hi, Monks! I tried to get answer from comp.lang.perl.misc, but I failed to get an ultimate answer. I just wrote short Perl script aiming at the processes below
step 1) read "20.xml" in my C:\perl folder
step 2) extract links in "20.xml"
e.g. extracting "Link A, B, C,..." from 20.xml
<permalink> Link A </permalink>
...
<permalink> Link B </permalink>
...
<permalink> Link C </permalink>
...
step 3) save links at "20.txt"

Below is what I have written until now.
I'm lost what line should be added, what should be corrected...
=================================================
#!/usr/bin/perl -w use strict; use LWP::Simple; use XML::TokeParser; use XML::Simple; my $t; my $cont = get("20.xml"); my $p = new XML::TokeParser(\$cont); $t = $p->get_tag("permalink"); $t = $p->get_trimmed_text("/permalink"); getstore($t, "20.txt");
=====================================================

If you had read this question from news group.I do apologize for repetition. If any monk knows the answer for this question, please let me know.
Thank you, always. A baby monk from Buffalo,NY.

Replies are listed 'Best First'.
Re: XML::TokeParser, how to use it??
by Tanktalus (Canon) on Jul 05, 2005 at 02:08 UTC

    Personal favourite is XML::Twig...

    use XML::Twig; use IO::File; my $twig = XML::Twig->new(); $twig->parsefile('20.xml'); # you can use parseurl if 20.xml really is + remote. my $localtxt = IO::File->new('20.txt', 'w') or die "Can't open 20.txt +for writing: $!"; foreach ($twig->get_xpath('//permalink')) { $localtxt->print($_->text(), "\n"); } $localtxt->close();
    Hope that helps.

      Tanktalus, sk and ikegami!
      Thank you very much for your help.
      It might sound funny that I had spent three days to think of my question.
      I sincerely thank you for your help, with all my heart.

      a little monk-to-be,

      Buffalo, NY.
Re: XML::TokeParser, how to use it??
by ikegami (Patriarch) on Jul 04, 2005 at 21:39 UTC
Re: XML::TokeParser, how to use it??
by sk (Curate) on Jul 04, 2005 at 21:47 UTC
    Do you just want to extract the links which are stored inside the permalink tags? If it is as simple as that I would just use a regex to do it -

    #!/usr/bin/perl -w while(<DATA>) { if (/permalink/) { s|<permalink>(.*)</permalink>|$1|i; print; } } __DATA__ <permalink> Link A </permalink> <tempNode> value1 </tempNode> SomeText <permalink> Link B </permalink> <tempNode2> value2 </tempNode2> <permalink> Link C </permalink>
    Output

    Link A Link B Link C

    If you are looking to parse the XML file cleanly for other purposes then you can check out XML::DOM. I don't have XML::TokeParser on my machine to check out code but XML::DOM should be easy to use. http://search.cpan.org/~tjmather/XML-DOM-1.43/lib/XML/DOM.pm

    cheers

    SK

Re: XML::TokeParser, how to use it??
by gellyfish (Monsignor) on Jul 05, 2005 at 07:59 UTC

    At a guess, this would look like RSS - if that is the case then it might be an idea for you to look at the module XML::RSS, of course I could be infering too much from the presence of the permalink element.

    /J\