Full code

#!/usr/bin/perl -w use strict; use XML::Simple; use XML::LibXML; use diagnostics; use Time::HiRes qw(gettimeofday); 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}) +; }; package main; #load xmlfile using XMLin (creating hashref) my $file = $ARGV[0]; my $reps = $ARGV[1] || 1; my $xml = XMLin( $file); our $dom; our %ELEMENTORDER = ( Process => 1, Filter => 2, Field => 3, ZName => + 4); my $before = gettimeofday(); my $xso; foreach my $i (0..$reps) { $xso = xmlsimple_output($xml); } my $xso_time = sprintf("%0.3f", (gettimeofday() - $before) * 1000); print "=========================XML Simple outptut =================== +===========\n"; print $xso; $before = gettimeofday(); my $lxo; foreach my $i (0..$reps) { $lxo = libxml_output($xml); } my $lxo_time = sprintf("%0.3f", (gettimeofday() - $before) * 1000); print "=========================Lib XML outptut ====================== +=======\n="; print $lxo; print "====================TIMINGS=======================\n"; print "LibXML time for $reps rep(s): $lxo_time ms\n"; print "XML Simple time for $reps rep(s): $xso_time ms\n"; #print keys %{$xml->{Process}->[0]->{LongDescription}->{content}}; #loads (above) into an XML::LibXML object using a recursive method sub element_order { my $element_name = shift; return $ELEMENTORDER{$element_name} || 9999999; } 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}) # foreach my $node (keys %{$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; } #use toString() to print it out #print add_nodes_hash($xml,0)->toString; 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); } sub xmlsimple_output { my $xml = shift; my $parser = MyXMLSimple->new( KeyAttr => [], RootName => 'Zymonic' ); return $parser->XMLout($xml, KeyAttr => [], RootName => 'Zymonic', NoEscape => 1, SuppressEmpty => 1 ); }

In reply to Re: Replacing XML::Simple XMLout with Lib::XML by amasidlover
in thread 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.