Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

XML Parsing Suggestions?

by Anonymous Monk
on May 10, 2005 at 17:45 UTC ( [id://455685]=perlquestion: print w/replies, xml ) Need Help??

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

I'm new to dealing with XML, so I thought I'd solicit some recommendations as to which modules are best suited for certain tasks. I have a couple of different tasks for a couple of different scripts, so feel free to suggest a different module for each need if you think that one module doesn't cover all of these nicely. What in your opinion is the best module (XML::Parser? XML::Twig? XML::XPath? XML::LibXML? XML::Simple? Other?) to use for each of these tasks? If you care to submit a code snippet along with your suggestion to demonstrate, I'd be most grateful as it'll probably help cut down my learning curve. =)

  1. Parse an XML file and retrieve all the comments.
  2. Parse an XML file and get a list of all values enclosed in a particular tag.
  3. Parse and XML file and get a list of all tags it contains.
  4. Parse an XML file, find and change a particular value inside a particular tag, the write the XML file back to disk. E.g. change "<greeting>Hi</greeting><greeting>Hello</greeting>" to "<greeting>Hi</greeting><greeting>Goodbye</greeting>".
  5. Retrieve content from an XML file based on a given XPath.

Thanks!

Replies are listed 'Best First'.
Re: XML Parsing Suggestions?
by stvn (Monsignor) on May 10, 2005 at 21:51 UTC

    Tasks 1-4 scream XML::SAX to me (which is kind of like dragonchilds suggestion of "XML::Parser in stream mode", but different). I say this because none of these tasks require you to load the entire XML document into a tree, nor do they require a heafty amount of contextual knowledge either.

    The nice thing about XML::SAX is that it can be PurePerl, or it can use a C based parser if it finds it. XML::Simple, while a very useful module, can be slow, and will build a structure of the entire XML doc for you, which is not what you need.

    Task 5 requires XPath, and I know very little about XPath, and even less about what modules support it, so I am afraid I cannot help there.

    -stvn
Re: XML Parsing Suggestions?
by dragonchild (Archbishop) on May 10, 2005 at 17:47 UTC
    *blinks* What have you tried?

    Personally, I always use XML::Parser in Stream mode, but that's because the tasks I've used XML for have lent themselves to that mode of thinking. I have never had to do any of those tasks, which makes me feel that this is somewhat homework-ish. If I ever did, I would get my employer to provide a commercial tool, similar to XMLSpy.


    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"

      *blinks* Nothing, yet. =) There are so many XML modules out there that I was just looking for recommendations as to which one(s) I should learn first for the kind of tasks I'm being asked to do. Opinions will vary from person to person I'm sure, but I thought some suggestions as to what other folks found most helpful might help me get my bearings. I did play around briefly with XML::Simple and XML::Parser in Object mode once a few months ago, but that was before my boss actually asked me to start dealing with XML documents and I didn't get a good feel for either of them in the limited time I had to play.

      Thanks for the XML::Parser in Stream mode suggestion. Is it possible to deal with comments in Stream mode? I thought the documentation made it sound like comments were basically ignored.

        Comments aren't useful data. If they were, they wouldn't be comments. :-)

        But, yes, you can parse comments in Stream mode. (If you couldn't, you couldn't get at them in any other mode as all XML::Parser modes are implemented as Stream mode.) The Comment handler (which is readily visible when you read the documentation is probably what you're looking for.


        • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
        • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
Re: XML Parsing Suggestions?
by scmason (Monk) on May 10, 2005 at 18:28 UTC
    I have played with XML::Simple recently. It seems to lend itself to simple XML files (thus the name). My recomendation on this is is that if you have very simple files (like config files) then this is your go. When the files have nested attributes, it is better to look upwards, like Parse in stream mode. Your xml file looks like this (with code tags)

    <greeting> Hi </greeting> <greeting> Hello </greeting> to <greeting> Hi </greeting> <greeting> Goodbye </greeting>
    Since this is a simple (almost config like) task, I would go with simple.

    With something like this:

    <greeting> How are you? <positive> <response> Fine </response> <response> OK </response> <reponse> Good </reponse> </positive> <negative> <response> Bad </reponse> <response> Not well </reponse> <negative> </greeting> <greeting>
    If this were the case, you would need something more complex than Simple provides. If you learn to use the most sophisicated method to begin with, it can scale up with you as your assignments (er, tasks, projects).
Re: XML Parsing Suggestions?
by Tanktalus (Canon) on May 10, 2005 at 23:33 UTC

    As with many questions, the more complex your requirements, the more complex the tool you need to deal with it. XML::Simple may do some of this, even possibly all of it. But whether it will work or not really depends on the structure of your document. Complex documents aren't handled, simple ones are, however, trivial. That's just the tradeoff.

    I first had to use perl with a complex XML document. So when I investigated all the options I could find at the time, I found XML::Twig, and found it could do everything I needed. I believe it can do all of the above, although I'm not sure how much of your XPath requirements it will meet - it's got good enough XPath support for my needs, but I can't tell if it'll meet yours.

Re: XML Parsing Suggestions?
by rg0now (Chaplain) on May 11, 2005 at 19:24 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://455685]
Approved by ghenry
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found