How can I create the XML::Simple data structure using a Perl XML SAX parser?

XML::Twig has a simplify method which you can call on a XML element which according to docs says.

XML::Twig has a simplify method which you can call on a XML element which according to docs says: Return a data structure suspiciously similar to XML::Simple's Here is an example: use XML::Twig; use Data::Dumper; my $twig = XML::Twig->new( twig_handlers => { rec => \&rec, } )->parsefile( 'data. Xml' ); sub rec { my ($twig, $rec) = @_; my $data = $rec->simplify; say Dumper $data; $rec->purge; } NB. The $rec->purge cleans out the record immediately from memory.

Running this against your XML example produces this: $VAR1 = { 'f1' => 'v1', 'f2' => 'v2' }; $VAR1 = { 'f1' => 'v1b', 'f2' => 'v2b' }; $VAR1 = { 'f1' => 'v1c', 'f2' => 'v2c' }; Which I hope is suspiciously like what comes out of XML::Simple :) /I3az.

As the author of XML::Simple, I'd just like to correct some misconceptions in your question. XML::Simple isn't a DOM parser, in fact it isn't a parser at all. It delegates all parsing duties to either a SAX parser or XML::Parser.

The speed of parsing will depend on which parser module is the default on your system. When you run 'make test' for the XML::Simple distribution, the output will list the default parser. If the default parser on your system is XML::SAX::PurePerl then it will be slow and more importantly buggy too.

If that's the case then I'd recommend installing either XML::Expat or XML::ExpatXS for an immediate speed up. (Whichever SAX parser is installed last will be the default from that point). Having said that, your requirements are a bit contradictory, you want something that returns your whole document as a hash and yet you don't want a parser that slurps the whole document into memory.

I understand your short-term goals, but as a longer term solution, I'd recommend migrating your code to XML::LibXML. It is a DOM parser but it's very fast because all the grunt work is done in C. Best of all the built-in XPath support makes it even simpler to use than XML::Simple - see this article.

(1) we have experimented with changing back-end parsers via $ENV{XML_SIMPLE_PREFERRED_PARSER}. It provided significant speedup, but NOT comparable to pure C solution (XPath module). So our conclusion was that a large amount of time is spent on building the entire data tree in memory as opposed to just parsing.

– DVK Jun 4 '10 at 21:13 (2) We don't want to return the whole document - we want an iterator that returns 1-level-deep tags as individual hashes, without storing the whole thing in memory (at least in Perl - not sure what the underlying XS module does) – DVK Jun 4 '10 at 21:15 (3) Unfortunately, the "correct" solution is not feasible - I CAN attempt to change the implementation of one module (whose API is basically "give me the next hashref"); but changing every single user of that module to use XPath is looking like too much of an effort to justify. Thus my looking for something that can emulate XML::Simple's putput per individual 2d level tag. – DVK Jun 4 '10 at 21:17 (4) ... and thanks for feedback!

One of the beauties of SO :) – DVK Jun 4 '10 at 21:17.

1 Downvote: â‘´ Link text does not match link target. ‘µ Neither Reader nor SAX fulfil the question's requirement. ‘¶ Grant already recommended LibXML one month ago.

– daxim Jun 27 '10 at 21:50 @daxim: XML::LibXML::Reader provides a 'pull' API to the libxml parsing library. This is an entirely different paradigm to the more commonly used DOM/XPath API of XML::LibXML and also seems like a good fit for your stated requirement of an 'iterator'. Don't dismiss it out of hand.

– Grant McLean Jul 19 '10 at 21:45.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions