Hello Monks, I have an XML file with this structure:
<document> <product> <date></date> <price></price> <content>heinous amount of unwanted text</conten> <color></color> </product> <product> <date></date> <price></price> <content>heinous amount of unwanted text</conten> <color></color> </product> </document>
Those <content> element contain huge amounts of text, as much as 80 MB. I need to truncate that text to, say 1-3MB, doesn't have to be exact. So I tried XML::Twig with like this:
XML::Twig->new( twig_roots => { content => \&content }, twig_print_out +side_roots => 1, keep_spaces => 1, ) ->parsefile( 'ginormous.xml'); exit; sub text { my( $t, $content) = @_; my $snipped = substr($content->text, 0, 1000000); $content->set_cdata($snipped); $t->flush; }
It kinda worked, but took, like, overnight. So I tried using XML:Sax to extract the one element and do the truncation, which worked great and took only a few minutes. So now I need to get rid of the <content> element in the original so I can plug the truncated stuff back into it. I thought this should work:
my $field= 'content'; my $twig= new XML::Twig( twig_roots => { $field => 1 }, twig_print_outside_roots => 1, twig_handlers => { $field => \&field } ); $twig->parsefile( "ginormous.xml"); sub field { my( $twig, $field)= @_; $field->delete; }
but it also took, like, overnight. How can I ignore <content> completely, and just print the rest?
Thanks!

In reply to Prune Twig From Huge XML File by andergoo

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.