in reply to Missing values in XML::Twig Output

When I run your code, it seems to create an output file which matches your requirements (web_bu.xml). The 1st display-name text has been replaced, and the other 2 remain the same:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/n +s/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:sch +emaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ +ns/j2ee/web-app_2_4.xsd"> <display-name>SOMEGUY</display-name> <display-name> Apache-Axis Servlet</display-name> <display-name> Axis Admin Servlet</display-name> </web-app>

How do you want the output file to look?

Replies are listed 'Best First'.
Re^2: Missing values in XML::Twig Output
by TJRandall (Sexton) on May 03, 2011 at 14:40 UTC
    Ugh - I forgot to say that - I want the output to be the first display-name to have changed to 'SOMEGUY', with the others still intact:
    <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/n +s/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:sch +emaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ +ns/j2ee/web-app_2_4.xsd"> <display-name>SOMEGUY</display-name> <servlet> <display-name> Apache-Axis Servlet</display-name> <servlet-name>AxisServlet</servlet-name> <servlet-class> org.apache.axis.transport.http.AxisServlet</servlet-class> </servlet> <servlet> <display-name> Axis Admin Servlet</display-name> <servlet-name>AdminServlet</servlet-name> <servlet-class> org.apache.axis.transport.http.AdminServlet</servlet-class> <load-on-startup>100</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/servlet/AxisServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>*.jws</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AdminServlet</servlet-name> <url-pattern>/servlet/AdminServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
      When I run your code, it generates two outputs:
      • some to STDOUT
      • other to file web_bu.xml
      My guess is that you want all output to go to the file and none to STDOUT. Is that correct? So, what you just posted should be the contents of the output file?
      use strict; use warnings; use XML::Twig; my $out_file = 'C:\web_bu.xml'; my $t = XML::Twig->new( twig_handlers => {'display-name' => \&convert}, pretty_print => 'indented' ); $t->parse(*DATA); open (my $fh_out, '>', $out_file) or die "unable to open '$out_file' f +or writing: $!"; $t->print($fh_out); # this prints to the filehandle sub convert { my ($t, $elt) = @_; my $txt = $elt->text(); if ($txt =~ /wtw_reports/i) { $elt->set_text('SOMEGUY'); } }
        That's correct - I want to write all of the contents of the original file to an XML file, replacing the value inside the first <display-name>.