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

Hi monks,

Can any body tell me how to parse an xml document using perl. I am new to xml. I have absolutely no idea of adding tags to xml. I have installed XML::Parser module from active perl repository.

Please help me.

Thanks in advance

Nalina

Replies are listed 'Best First'.
Re: Perl xml parser
by astroboy (Chaplain) on Aug 02, 2004 at 05:30 UTC
Re: Perl xml parser
by tachyon (Chancellor) on Aug 02, 2004 at 05:24 UTC

    You may find XML::Simple meets your needs with a minimum of complexity.

    cheers

    tachyon

Re: Perl xml parser
by davidj (Priest) on Aug 02, 2004 at 05:19 UTC
    Since you have installed XML::Parser via ppm, I assume you plan on making use of it. In that case, your best first step would be to perldoc XML::Parser.

    davidj

Re: Perl xml parser
by davorg (Chancellor) on Aug 02, 2004 at 08:35 UTC

    You've had some good advice from people already, but really it's hard to give any solid advice without knowing what you want to do. Just saying you want to parse a document doesn't really help. What do you want to do with the data once you've parsed it?

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Perl xml parser
by gaal (Parson) on Aug 02, 2004 at 08:25 UTC
    I agree with everyone's replies above, especially that you should read up on XML::Simple first.

    In my experience, XML::LibXML is very fast, but you should only care about this if you are parsing a lot of data (say, hundreds of kilobytes). Installing it is a bit trickier than other XML modules since it has more dependencies, and is pretty sensitive about versions of its dependencies, but I thought it's worth mentioning because I think it's relatively unknown and, well, because it *is* fast :)

      XML::LibXML also offers much simpler and clearer interface than XML::Parser. I'd never use XML::Parser directly; at the least, you need XML::Twig to get any work done.

      Another serious reason in favour of libxml, which is much more practically important than its performance, is that consumes much less memory — a huge win when you're working with larger XML files. (It's no fun when your Perl processes grow to 600MB to generate a mere 40MB XML file.)

      On unixoid systems, the dependencies are not likely to be a problem either. I don't know if there's a PPM for Windows people, but if so, that should work with a minimum of fuss for them.

      Makeshifts last the longest.

        Hmmm, maybe the performance increases I saw were memory related. For some reason I had not thought to look at that! Moving from XML::Twig to XML::LibXML gave me a 36% speedup in a function that was building a complex structure from raw XML data.

        I've gotten the module to work with Cygwin (perl and libs). I did need to make use of rebaseall to get my DLLs in order, though; plus there's a tacit dependency on libiconv (tacit == nobody tells you this, but the module fails to build).

        On Solaris I encountered two problems. The first was that http://www.sunfreeware.com/ packages a version of libxml2 that is incompatible with the Perl bindings. I solved that (painfully) by making a package of my own. (If anyone wants a copy, give me a holler; but I cannot support it.) The second was a trivial compilation error in one file (was it memory management?) in the bindings plus pesky configuration minutiae. I sent a patch to the maintainers and I don't know if it's been incorporated since.

Re: Perl xml parser
by danielcid (Scribe) on Aug 02, 2004 at 12:30 UTC
    There is also the XML::Smart. It's very easy to use and with some extra features that XML::Simple does not have.

    -DBC