ials
ials

Reputation: 39

How to add a fixed number to a particular attribute of a line in an XML file?

I have an XML file that contains several lines like this:

<Teste  Time="380643" TT="380592" Win="-2" Xl="28" Yl="55" />
<Teste  Time="380660" TT="380592" Win="-2" Xl="28" Yl="55" />

I need to add a fixed number to every instance of the Time attribute, e.g. 380643 + 10000, and get an XML file with the new lines, e.g. <Time="390643" TT="380592" Win="-2" Xl="28" Yl="55"/>. Is this possible using Perl? If so, how? If not, what should I do?

Upvotes: 1

Views: 64

Answers (2)

Alex Shesterov
Alex Shesterov

Reputation: 27525

Use XML::Twig with twig_roots:

my $twig=XML::Twig->new(   
  twig_roots => 
    { Time => sub { $_->set_att( TT => $_->att('TT')+10000 )->flush; } 
    },
  twig_print_outside_roots => 1 
);
$twig->parsefile('myXmlFile.xml');

Upvotes: 1

Yash
Yash

Reputation: 177

You can do something like this :

#!/usr/bin/perl

# use module
use XML::Simple;

# create object
$xml = new XML::Simple;

# read XML file
$data = $xml->XMLin("data.xml");

# access XML data
$time = $data->{Time}->TT;
//do your stuff here

More : Here

Upvotes: 0

Related Questions