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

ok... as the title reads, this is probably a n00b question to most, but here goes...

I've created a script that converts an XML feed from blogger or other blog sites, and displays it using the template i have created... the only problem is... i can't figure out how to get the XML feed into an array.

simply put, I want http://www.example.com/atom.xml to be loaded into @feed.

I can do it if i save the XML feed to my server (using the open( ) function), but i don't want to do that...

hopefully i haven't offended anyone with the stupidity of this question, and I wait to hear from u all soon!

BTW: here's something else i've been working on... note: it's not on 24/7. http://frequency.hopto.org (I created the whole site... gallery and admin section too!)

Retitled by davido from 'Perl n00b Qusetion'.

Replies are listed 'Best First'.
Re: How do I capture XML into a variable?
by friedo (Prior) on Apr 19, 2005 at 01:23 UTC
    I'm not sure what you mean by "get the XML feed into an array." How are you retrieving the XML feed in the first place? With LWP or something else? In what format do you have it currently?

    I'll go out on a limb at assume that because you mentioned opening a file, you want the XML feed as one line per element in the array. That will probably make it somewhat difficult to parse, though. However, assuming you already have the XML in a scalar, you can use split to split it up on newlines ("\n").

      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...
      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
        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.

        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.

Re: How do I capture XML into a variable?
by inman (Curate) on Apr 19, 2005 at 07:57 UTC
    Since you are working witeh RSS Feeds then you should take a look at the modules designed for the purpose. I have been using XML::RSSLite to parse RSS feeds into an internal data structure.