in reply to How to substitute a word with an other?

Here's something to get you started with reading XML files with XML::Simple.

#!/usr/bin/env perl use warnings; use strict; use XML::Simple qw/:strict XMLin/; my $xmldata = XMLin 'FILENAME.xml', ForceArray=>[], KeyAttr=>{};

... and you'll have the data from the XML file in $xmldata. To take a look at what that Perl data structure looks like, you can do this:

use Data::Dumper; print Dumper($xmldata);

Then, you would use the methods described in the sections "Access and Printing of ..." in perldsc to access the data structure in $xmldata (also read the "Common Mistakes" section, and really, the rest of the documentation too ;-)).

One thing to note about XML::Simple is that in order to be "simple" to use, it simplifies the XML's structure a bit. For simple XML files it'll work very well, but if you have a complex input file or need to preserve the structure of the input file, that module may be too simple, in which case you can look at the many other XML modules on CPAN, such as XML::LibXML or XML::Twig.

This is just an example of how to read the XML file. What you then do depends very much on what the XML file looks like, and what you want your program to do. Try it out, write some code, and if you need any help please feel free to ask, and provide sample input, your code with a description of what it's supposed to do, any error messages, and desired output (see also How do I post a question effectively?).

Replies are listed 'Best First'.
Re^2: How to substitute a word with an other?
by fx3000 (Initiate) on May 16, 2014 at 15:30 UTC

    Hi and thanks for your answer, I will try to be more effectively in my question! ;-) I have a file (File.txt in my code) with many lines like this one:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ServerName>RIGHTb4024</ServerName><DateTime>2013-04-22 10:53:22</DateTime><Application>SFMWW</Application><Function>Query</Function><UserNameOS>TESTER001</UserNameOS><UserNameApp>TESTER001</UserNameApp><LocationIP>17 2.31.8.71</LocationIP><LocationMachineName>RIGHTb4024</LocationMachineName><LocationMachineType/><LocationSessionID/><LocationRemoteIP/><LocationRemoteMachineName/><Lo cationRemoteUserName</LocationRemoteUserName><Customer>123456789016</Customer><Product>001007</Product><Operation>DET</Operation></SOAP-ENV:Body></SOAP-ENV:En velope>

    And what I need is to have an output file (Output.txt in my code) with a specific field value changed the like this

    <UserNameApp>TESTER001</UserNameApp>

    with

    <UserNameApp>Frank.Sinatra</UserNameApp>.

    Following a specific association in file Address.txt :

    TESTER001,Frank.Sinatra

    TESTER002,Cyndy.Crawford

    and so on .. Thanks for your help.

      Perhaps XML::Rules and its filter* methods would work for you?

      PerlMonks isn't a code writing service, but if you post the code you're having problems with, we'll be happy to help.