in reply to Re^4: Removing XML comments with regex
in thread Removing XML comments with regex
As a very rough benchmark, I created a ~20MB xml file (I took the doc in the OP, and copied the middle part over and over). I filtered the doc using XSLT (using XML::LibXSLT, and the XSLT in the node above), and XML::Twig (the solution elsewhere in this thread). The XSLT took about 3 seconds, the XML::Twig took about 20. Both used vast quantities of memory.
And though I'm not familiar with XML::Parser::Expat, I hacked together something which seems to work (though I am likely missing something for some types of XML content), and ran in about 3 seconds without using much memory at all.
Update: repeated XSLT with 40MB file, took just a few more seconds.xslt.pl: #!/usr/bin/perl use strict; use warnings; use XML::LibXML; use XML::LibXSLT; my $parser = XML::LibXML->new(); my $xslt = XML::LibXSLT->new(); my $source = $parser->parse_file('tmp.xml'); my $style_doc = $parser->parse_string(<<EOT); <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" versi +on="1.0"> <xsl:strip-space elements="*"/> <xsl:output method="xml" indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()" /> </xsl:copy> </xsl:template> <xsl:template match="comment()" /> </xsl:stylesheet> EOT my $stylesheet = $xslt->parse_stylesheet($style_doc); my $results = $stylesheet->transform($source); print $stylesheet->output_string($results); ---------------------------------------------- twig.pl: #!/usr/bin/perl use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new (comments => 'drop', pretty_print => 'indent +ed'); $twig->parsefile("tmp.xml"); $twig->print(); ---------------------------------- expat.pl: #!/usr/bin/perl use strict; use warnings; use XML::Parser::Expat; my $parser = new XML::Parser::Expat; $parser->setHandlers('Start' => \&sh, 'End' => \&eh, 'Char' => \&ch); $parser->parsefile('tmp.xml'); sub sh { my ($p, $e) = @_; print $p->recognized_string(); } sub eh { my ($p, $e) = @_; print $p->recognized_string(); } sub ch { my ($p, $s) = @_; print $s; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Removing XML comments with regex
by Jenda (Abbot) on Dec 28, 2007 at 22:02 UTC | |
|
Re^6: Removing XML comments with regex
by Anonymous Monk on Dec 28, 2007 at 21:02 UTC | |
by runrig (Abbot) on Dec 28, 2007 at 22:16 UTC | |
by mirod (Canon) on Dec 29, 2007 at 03:51 UTC | |
by runrig (Abbot) on Dec 29, 2007 at 04:25 UTC | |
by Jenda (Abbot) on Jan 02, 2008 at 23:37 UTC |