andergoo has asked for the wisdom of the Perl Monks concerning the following question:
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:<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>
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: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; }
but it also took, like, overnight. How can I ignore <content> completely, and just print the rest?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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Prune Twig From Huge XML File
by mirod (Canon) on Mar 16, 2009 at 20:05 UTC | |
by andergoo (Initiate) on Mar 16, 2009 at 20:54 UTC | |
by mirod (Canon) on Mar 16, 2009 at 21:44 UTC | |
by Anonymous Monk on Mar 17, 2009 at 08:59 UTC | |
|
Re: Prune Twig From Huge XML File
by Jenda (Abbot) on Mar 16, 2009 at 23:52 UTC | |
by Anonymous Monk on Mar 17, 2009 at 09:09 UTC | |
|
Re: Prune Twig From Huge XML File
by mirod (Canon) on Mar 17, 2009 at 09:50 UTC | |
by andergoo (Initiate) on Mar 18, 2009 at 06:13 UTC |