On 18/04/2011 21:55, Rajpreet wrote:
Thanks for your replies. But the above message is jst a sample and the
exact message we get is pretty huge(its a trading sysem message)... I
do have XML Parser installed.. I was trying to format a sample message
using start and a default handler. I do get data in a variable and can
change it, but how do I get the changed XML message in a new file. My
question might be confusing or silly, but thats where I am currently
stuck . I just wrote a dummy script-

#!/usr/bin/perl
use XML::Parser;
use Data::Dumper ;
open(XML, "a.txt") || die("Could not open file XML_FILE !"); ### handler
         my $Data =<XML>;
         close(XML);
&ReplaceXMLValue($Data,'MOSP_Trader_Code','1111111111111');
sub ReplaceXMLValue
{
          my $Data       = shift @_;
          my $tagName    = shift @_;
          my $NewValue   = shift @_;
        my $parser = new XML::Parser ( Handlers =>  {   # Creates our parser 
object
                               Start   =>  \&hdl_start,
                                Default =>  \&hdl_default,
                             });
   $parser->parse($Data);

print " \n\n\n Formatted data is - $Data \n\n\n";
}
   # The Handlers
   sub hdl_start{
       my ($p, $elt, %atts) = @_;

       return unless $elt eq 'MOSP_Trader_Code';  # We're only interrested in 
what's said
$flag=1;
   }
sub hdl_default {
     my ($p, $data) = @_;
     if ($flag  == 1)
     {

print "Inside default handler \n";
if ($data eq 'CHL')
{
print "chaging data value form CHL to POST\n";
         $data ='POST';
         print "$data\n";
$flag =0;
     }
}
}  # End of default_handler

There is a 'Style' option to XML::Parser which, if set to 'Stream', will
cause the symbols in the parsed XML to be normalised and printed to
STDOUT. If appropriately-named subroutines exist, then instead of
sending the symbol to output, the subroutine will be called with the
value in $_.

If there is a procedure called Text(), it will be called each time a
text item in the XML is encountered, and so it could examine the context
and the content of $_ and modify it if necessary before calling print.

The program below uses this technique to perform the transformation that
you have described. Note that the XML::Expat object is passed to Text()
as the first (and only) parameter, and can be used to establish the name
of the current element.

HTH,

Rob

use strict;
use warnings;

use XML::Parser;

ReplaceXMLValue(*DATA, 'MOSP_Trader_Code', 'CHL', 'POST');

sub ReplaceXMLValue {

  no warnings 'closure';

  my ($fh, $tag_name, $old_value, $new_value) = @_;

  my $parser = new XML::Parser(Style => 'Stream');

  sub Text {
    my $expat = shift;
    if ($expat->current_element eq $tag_name) {
      s/\A(\s*)\Q$old_value\E(\s*)\z/$1$new_value$2/;
    }
    print;
  };

  $parser->parse($fh);
}

__DATA__

<myxml>
  <dataset id="1">
    <field1>
      Field 1 Data
    </field1>
    <field2>
      Field 2 Data
    </field2>
    <MOSP_Trader_Code>CHL</MOSP_Trader_Code>
    <field3>
      Data associated with field 3
    </field3>
    <field4>
      Data for field 4
    </field4>
  </dataset>
</myxml>

**OUTPUT**

<myxml>
  <dataset id="1">
    <field1>
      Field 1 Data
    </field1>
    <field2>
      Field 2 Data
    </field2>
    <MOSP_Trader_Code>POST</MOSP_Trader_Code>
    <field3>
      Data associated with field 3
    </field3>
    <field4>
      Data for field 4
    </field4>
  </dataset>
</myxml>


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to