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

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

  • Comment on Re: How do I capture XML into a variable?

Replies are listed 'Best First'.
Re^2: How do I capture XML into a variable?
by tubes41 (Initiate) on Apr 19, 2005 at 01:50 UTC
    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...
Re^2: How do I capture XML into a variable?
by tubes41 (Initiate) on Apr 19, 2005 at 01:29 UTC
    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.

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