in reply to Re: How do I capture XML into a variable?
in thread How do I capture XML into a variable?

ok... lets see if i can clarify this... blogger outputs the xml feed to your blog address here: http://www.username.blogspot.com/atom.xml now... i need that file loaded into an array so that my perl script can access it. hopefully that's not too confusing... TIA tubes41
  • Comment on Re^2: How do I capture XML into a variable?

Replies are listed 'Best First'.
Re^3: How do I capture XML into a variable?
by friedo (Prior) on Apr 19, 2005 at 01:41 UTC
    Unfortunately all you have done is confuse the situation further. You're not going to get very far if you can't figure out what your question actually is. Ask yourself these:

    1. What, specifically do you mean when you say "loaded into an array." What do you want this array to look like? How do you intend to use it?
    2. What is "atom.xml"? Is this a local file? Is it on a remote server somewhere?
    3. What do you intend to do with atom.xml once you have it? Perhaps "loading into an array" is not the ideal solution and the people here can suggest something better. There are a plethora of modules out there for parsing XML.

    A good bet is that a lot of the people who can answer your questions don't know what Blogger is or how it works. (I sure don't.) So explain exactly what you need.

      OMG OMG OMG.... thankyou for mentioning LWP... I did a little searching on perl.org and it's just what i wanted.... Thankyou, thankyou, thankyou...

      ok.. here's what i did.

      use LWP::Simple;
      $scalar = get "http://www.frequencyyouth.blogspot.com/atom.xml";
      @feed = split("\n", $scalar);

      again... thankyou... i've been looking for that answer for ages...

        First off, try using <code> tags for your code.

        Second, when dealing with XML, it's highly recommended to use an XML parser of some sort. My favourite is XML::Twig, but others exist. There probably is even a wrapper around the XML parsers that help parse RSS feeds. Once you've retrieved the data into your $scalar, you can put it into these parsers and be able to get the data you want much more easily.

        #!/usr/bin/perl -w use LWP::Simple; use XML::Twig; my $twig = XML::Twig->new(); $twig->parse(get 'http://www.frequencyyouth.blogspot.com/atom.xml'); my @entries = $twig->get_xpath('entry'); foreach my $entry (@entries) { my @title = $entry->get_xpath('title'); print "Found ", $_->text(), "\n" for @title; my @content = $entry->get_xpath('content'); print " -> ", $_->text(), "\n" for @content; }
Re^3: How do I capture XML into a variable?
by graff (Chancellor) on Apr 19, 2005 at 01:48 UTC
    I guess the crux of the question still stands: Why an array? That is, as opposed to a single scalar string? or a data structure populated by an XML parsing module? Perl can access the content in these other forms too, and possibly to better effect -- unless you have some specific plan in mind that makes the array a better approach.

    If you really are talking about a data file (i.e. on local disk), you can slurp the whole file into a scalar, or read it line-by-line into an array, or (best yet) parse it into a data structure. Maybe if you show a little bit of code that you're trying out, or at least some pseudo-code that describes what sort of processing you want to accomplish (what does input look like, what does output look like, etc), we'll understand what you're talking about.