#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper; use XML::Simple; use XML::Parser;
$Data::Dumper::Indent = 1; $Data::Dumper::Terse = 0;
sub get_seleted_value { my ( $property, $search_value, $returned_key ) = @_;
my $wanted = "";
# $property is an array ref, whose elements are hash ref.
# Get array index based on $search_value, and return the value
# of $returned_key.
foreach my $item ( @$property ) { my $name = $item->{'NAME'}; my $value = $item->{$returned_key}; if ( $name eq $search_value ) { $wanted = $value; last; } }
return $wanted; }
sub modify_keybinds_value { my ( $xml_file, $iparam_name, $which_key, $set_value ) = @_;
my $xml_ref = XMLin($xml_file,KeepRoot => 1, ForceArray => 1); #print Data::Dumper->Dump([$xml_ref],["*xml_file"]);
my $iparam_array = $xml_ref->{CIM}->[0]->{MESSAGE}->[0]->{SIMPLEREQ} ->[0]->{IMETHODCALL}->[0]->{IPARAMVALUE};
# get ObjectName
my $object = get_seleted_value($iparam_array, $iparam_name, "INSTANCENAME"); my $keybinds_array = $object->[0]->{KEYBINDING}; # get string name
my $keyvalue = get_seleted_value($keybinds_array, $which_key, "KEYVALUE"); print "$xml_file: old value is $keyvalue->[0]->{content}\n"; $keyvalue->[0]->{content} = $set_value; print "$xml_file: new value is $keyvalue->[0]->{content}\n"; open ( my $fd, ">$xml_file" ) || die "can't open file\n";
my $xml_out = XMLout($xml_ref,KeepRoot => 1,NoSort => 1, XMLDecl => "", OutputFile => $fd );
close $fd; }
my $filename = "host.xml"; modify_keybinds_value($filename, 'InstanceName', 'CreationClassName', "HostSystem"); modify_keybinds_value($filename, 'InstanceName', 'Name', '');
|