in reply to Re: Missing values in XML::Twig Output
in thread Missing values in XML::Twig Output

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>

Replies are listed 'Best First'.
Re^3: Missing values in XML::Twig Output
by toolic (Bishop) on May 03, 2011 at 15:08 UTC
    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>.
        That's what my code does. Give it a try.