As per accepted wisdom we've replaced XMLin with XML::LibXML - there are places where its made life easier and places where its made life harder but overall its been a positive change (its also quite a lot faster.)

However, we're now looking at XMLout - we currently do XMLout($some_big_hashref, options) and then put the output through XSL - and before anyone suggests JSON, Template::Toolkit etc. as alternatives; we do use those but in other parts of our system.

So I got a newly recruited developer to knock together a 'quick and dirty' test script to see a) how to do it and b) the performance impact. We started with an XML::Simple section as a benchmark:

package MyXMLSimple; use base 'XML::Simple'; sub sorted_keys { my ( $self, $name, $hashref ) = @_; return sort { ma +in::element_order($a) <=> main::element_order($b) } keys(%{$hashref}) +; }; ... SNIP ... return $parser->XMLout($xml, KeyAttr => [], RootName => 'Zymonic', NoEscape => 1, SuppressEmpty => 1 );

Then we did a LibXML version...

sub add_nodes_hash { #first time $xml will be the file, after that it will be the hash +or array ref # $is_child is a flag to see if it wants to be added as like a roo +t node, or part of a nest my $xml = shift; my $parent_element = shift; foreach my $node (sort {element_order($a) <=> element_order($b)} k +eys %{$xml}) { #next if ($node =~/\//); if (ref($xml->{$node}) eq 'HASH') { my $element = $dom->createElement( $node ); $parent_element->insertAfter($element,undef); add_nodes_hash($xml->{$node},$element); } if (ref( $xml->{$node} ) eq 'ARRAY') { foreach my $array_element (@{$xml->{$node}}) { if (ref($array_element) eq 'HASH') { my $element = $dom->createElement( $node ); my @attributes = $element->attributes(); $parent_element->insertAfter($element,undef); add_nodes_hash($array_element,$element); } elsif (!ref( $xml->{$node} ) && ($xml->{$node})) { my $element = $dom->createElement( $node ); $element->appendText($xml->{$node}); $parent_element->insertAfter($element,undef); } } } elsif (!ref( $xml->{$node} ) && ($xml->{$node})) { my $element = $dom->createElement( $node ); $element->appendText($xml->{$node}); $parent_element->insertAfter($element,undef); } } return $dom; } sub libxml_output { my $xml = shift; #create the dom object $dom = XML::LibXML::Document->new(); my $root = $dom->createElement('Zymonic'); $dom->setDocumentElement($root); add_nodes_hash($xml, $root); return $dom->toString(1); }

When we generate 3000 lines of XML (approximately) we get the following timings.

====================TIMINGS======================= LibXML time for 100 rep(s): 5409.750 ms XML Simple time for 100 rep(s): 3699.437 ms

I'll put the full test script code in a reply to this node in case it helps - but didn't want to add it all for brevity

Now before I ask my specific questions, I'll add some caveats / further info:

Finally, my actual question(s)... Have we made a fundamental error somewhere and there is a way of going from XMLout to something 'better' whilst retaining equivalent (or better!) performance and keeping the code nice and simple? Ideally a method that we can use a one liner to go from nested hashref to XML output.


In reply to Replacing XML::Simple XMLout with Lib::XML by amasidlover

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.