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

Hi forks, I am working on how to parse an xml file and store the contents into a pretty complicated data strctures like a hash of hash.... I would like to use XML:Parse's Object style to do this, but don't have experience with XML:Parse, can someone give me an example on this. Thanks in advance.

Edit kudra, 2002-04-18 Changed title

  • Comment on Example of XML::Parse object style requested

Replies are listed 'Best First'.
Re: XML::Parser
by dsb (Chaplain) on Apr 17, 2002 at 20:49 UTC
    Okay, first of all:
    $ perldoc XML::Parser

    Second of all,

    XML::Simple
    will accomplish this for you and it will do so much easier. So:
    $ perldoc XML::Simple
    And a quick synopsis:
    #!/usr/bin/perl use XML::Simple; use strict; # use strict, always use strict $x = new XML::Simple(); $xref = $x->XMLin('/path/to/xmlfile'); # $xref now stores your xml in a hashref

    I strongly suggest you read the docs above though because there are a few attribute options for the <text>XML::Simple</text> object that you will want to at least know about.




    Amel

      You might want to ask a little bit more about the problem before recommending XML::Simple. XML::Simple works great for data-oriented XML, but as soon as you have to deal with documents, that usually include mixed content (<p>this is <b>mixed</b> content</p>: text and sub-elements mixed within an element) then you just can't use it. The content field of the hash can only hold one text, not several as is the case with mixed content.

      Plus what if the data to be processed is 4Gb of XML log?

      That said there are no tutorials that I am aware of on the Object style of XML::Parser.

      Plus XML::Parser is not really the favored way to process XML any more (read it is not actively maintained and its interface is not standard): XML::libXML, or XML::XPath, or (shameless plug!) XML::Twig are good alternatives. Plus of course XML::Simple if your XML is nice enough to fit in it ;--)

      Thank you. XML::Simple is good enough for my project.
Re: Example of XML::Parse object style requested
by mslattery (Initiate) on Apr 18, 2002 at 14:51 UTC
    Hello,

    Just wanted to offer my 10 cents from palying around with HoH (Hash of Hashes) with XML::Dumper / XML::Parser.

    Basically didn't play very well with HoH, I use XML::Dumper to output the HoH, but use a custom loader module for the re-reading of the HoH.

    It may work well, but I unfortunatly did not find the correct attributes to get it working with HoH.

    Plus my little XML database wasn't that hard to make a loader module for.

    Best of Luck,
    mslattery