Dear Experts,
Here a piece of my codes:
And below is a test run:
Qustions:
1) How do I retain all the original tags?
2) More importantly, why does the 1st line change? How to keep it the same?
Many thanks!!
Here a piece of my codes:
Code:
use Data::Dumper;
use XML::Simple;
my $srce = 'env.xml';
my $data = XMLin($srce);
print Dumper( $data );
foreach my $k1 (keys(%{$data->{entry}})) {
foreach my $k2 (keys(%{$data->{entry}->{$k1}})) {
$data->{entry}->{$k1}->{$k2} =~ s/unassigned/cyan/g;
}
}
my $dest = 'newenv.xml';
XMLout($data, NoAttr=>1, XMLDecl=>1, OutputFile=>$dest);
my $content1 = `cat $srce`;
my $content2 = `cat $dest`;
print "===================================\n";
print "$content1\n";
print "-----------------------------------\n";
print "$content2\n";
print "===================================\n";
Code:
% ./xml.pl
$VAR1 = {
'entry' => {
'ec2-ssh-key-name' => {
'content' => 'unassigned'
},
'credentials-file' => {
'content' => 'Credentials.properties'
},
'ec2-instance' => {
'content' => 'i-1ccb2973'
},
'ec2-ssh-key-path' => {
'content' => 'unassigned.pem'
}
}
};
===================================
<?xml version="1.0" [b]encoding="UTF-8"?[/b]>
<properties>
<entry key="credentials-file">Credentials.properties</entry>
<entry key="ec2-ssh-key-name">unassigned</entry>
<entry key="ec2-ssh-key-path">unassigned.pem</entry>
<entry key="ec2-instance">i-1ccb2973</entry>
</properties>
-----------------------------------
<?xml version='1.0' [b]standalone='yes'?[/b]>
<opt>
<entry>
<name>credentials-file</name>
<content>Credentials.properties</content>
</entry>
<entry>
<name>ec2-instance</name>
<content>i-1ccb2973</content>
</entry>
<entry>
<name>ec2-ssh-key-name</name>
<content>cyan</content>
</entry>
<entry>
<name>ec2-ssh-key-path</name>
<content>cyan.pem</content>
</entry>
</opt>
===================================
Qustions:
1) How do I retain all the original tags?
2) More importantly, why does the 1st line change? How to keep it the same?
Many thanks!!