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; }
In reply to Prune Twig From Huge XML File by andergoo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |