Learned Men of the Perl Realm:

I am writing a perl program to remove certain servlet tags from a web.xml file. One attempt (see listing) seems to work well except that comments are not preserved.

The attempt below preserves comments but mangles the XML prolog. What can I do to overcome this?

------------------ Perl Code ---------------- #!/usr/bin/perl use strict; use XML::Twig; my $twig= new XML::Twig(TwigRoots => { servlet => 1 }, TwigHandlers => { servlet => \&servletTag}, + TwigPrintOutsideRoots => 1 ); $twig->set_pretty_print( "indented"); $twig->parsefile( "web.xml"); $twig->flush(); 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; } } } ------------------ XML Input File ------------ <?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> ------------------ Program Output ------------ <?xml version="1.0" encoding="ISO-8859-1"?>><!-- This is my comment -- +><web-app> </web-app> <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>

In reply to XML::Twig Mangles XML prolog by gjeffrey

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.