Dear Monks,
I am doing a small proof of concept to see if XML::Twig will work for me, i.e. process large XML files in a reasonable amount of time. I use the script below to generate simple XML files for testing. I vary the value for $num_departments to obtain different sizes. A value of 1 mio produces an XML file of about 688 MB.

use strict; use warnings; my $file_name = "dharry.xml"; my $num_departments = 1000; open (XML_OUT_FILE, ">$file_name") or die "Could not open $file_name\n"; print XML_OUT_FILE "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; print XML_OUT_FILE "<Company>\n"; for (my $i=0; $i<$num_departments; $i++) { print XML_OUT_FILE " <Department>\n"; print XML_OUT_FILE " <Name>Bla$i</Name>\n"; for (my $j=0; $j<5; $j++) { print XML_OUT_FILE " <Person id=\"$i$j\">\n"; print XML_OUT_FILE " <First>John$i$j</First>\n"; print XML_OUT_FILE " <Last>Doe$i$j</Last>\n"; my $phone_ext = int(rand(10000000)); print XML_OUT_FILE " <PhoneExt>$phone_ext</PhoneExt>\n" +; print XML_OUT_FILE " </Person>\n"; } print XML_OUT_FILE " </Department>\n"; } print XML_OUT_FILE "</Company>\n"; close XML_OUT_FILE or die "Could not close $file_name\n"; print "Done...\n";

I have run many different tests, this is one of them that fails. I attempt to do a smart update: one Twig only, based on a specific value of the id attribute on the Person element.

use strict; use warnings; use XML::Twig; # Select Twig bassed on value of id attribute on Person element my $twig= new XML::Twig( twig_handlers => { 'Person[@id="50000"]' => \&Person } ); $twig->set_pretty_print ('record'); # Human readable output please $twig->parsefile( "dharry.xml"); $twig->flush; sub Person { my( $twig, $person)= @_; my $name = $person->first_child("First"); $name->set_text("dHarry"); $twig->flush; }

Results

$num_departments xml file size before xml file size after Time usage
1000 657 KB 708 KB seconds
10_000 6.57 MB 7.07 MB 1 minute
100_000 67.2 MB n/a n/a

I was a bit surprised by the crashing of the program. I tried different xpath expressions and rerun the test. Sometimes the 67.2 MB file was processed successfully but bigger files could not be handled. Note that the resulting xml files get a bit bigger because of the pretty_print option. Any ideas why it isn’t working? Is my code wrong?

NB
  1. XML::Twig v3.32 running on Windows, Perl 5.88 ActiveState.
  2. I have also run tests with parsefile_inplace which does not seem to make a difference.

In reply to Putting XML::Twig to the test by dHarry

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.