gjeffrey has asked for the wisdom of the Perl Monks concerning the following question:

Monks Of The Perl Realm:

I am writing a perl program to remove certain servlet tags from a web.xml file. Below is one approach that I have taken. The problem is that it doesn't print the XML comments. Is there anything I can do to keep the comments?

Notice also that the XML file prolog has been modified. How do I prevent that?

------------------ Perl Code ------------------ #!/usr/bin/perl use strict; use XML::Twig; my $twig= new XML::Twig(TwigHandlers => { servlet => \&servletTag }); $twig->set_pretty_print( "indented"); # parse the twig $twig->parsefile( "web.xml"); $twig->print(); sub servletTag { my $LOCALE = "es_US"; my( $twig, $servlet)= @_; my $jspFileTag= $servlet->first_child("jsp-file");; if (defined($jspFileTag)) { my $path = $jspFileTag->text(); my $LOCALE_MOD = "/$LOCALE/"; if ( $path =~ /$LOCALE_MOD/ ) { $servlet->cut(); return; } } } ------------- Sample Web.xml --------------------- <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// +EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <!-- This is my comment --> <web-app> <servlet> <servlet-name>Authenticate_en_US</servlet-name> <jsp-file>/vxml/en_US/type_0/entry/Authenticate.jsp</jsp-file> <load-on-startup>2</load-on-startup> </servlet> <servlet> <servlet-name>Authenticate_es_US</servlet-name> <jsp-file>/vxml/es_US/type_0/entry/Authenticate.jsp</jsp-file> <load-on-startup>2</load-on-startup> </servlet> </web-app> ------------------ Perl Output ------------------- <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> <!DOCTYPE web-app SYSTEM "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>Authenticate_en_US</servlet-name> <jsp-file>/vxml/en_US/type_0/entry/Authenticate.jsp</jsp-file> <load-on-startup>2</load-on-startup> </servlet> </web-app>

Replies are listed 'Best First'.
Re: Comments in XML::Twig
by iburrell (Chaplain) on Jul 26, 2004 at 20:45 UTC
    I remember correctly, XML::Twig has options that control the processing of comments. 'keep' writes them in the output, and 'process' adds them to the tree as special elements.
    XML::Twig->new(comments => 'process');
      Thanks. This works well.

      Any ideas on why it is reformatting the prolog: it added standalone="no" to the xml declaration, and in the DOCTYPE changes PUBLIC to SYSTEM even if I disable DTD loading with

      LoadDTD => 0