Chinaunix首页 | 论坛 | 博客
  • 博客访问: 455877
  • 博文数量: 45
  • 博客积分: 2526
  • 博客等级: 少校
  • 技术积分: 478
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-12 21:04
文章分类

全部博文(45)

文章存档

2014年(1)

2011年(1)

2010年(3)

2009年(22)

2008年(18)

我的朋友

分类:

2009-06-24 09:21:20

利用XML::Simple来操作XML文件,看不明白的请参考:
特别要注意KeepRoot,ForceArray,XMLDecl等option
 
测试用的XML文件: host.xml
 
 

<?xml version='1.0'?>
<CIM CIMVERSION="2.0" DTDVERSION="2.0">
  <MESSAGE ID="90005" PROTOCOLVERSION="1.0">
    <SIMPLEREQ>
      <IMETHODCALL NAME="GetInstance">
        <LOCALNAMESPACEPATH>
          <NAMESPACE NAME="root" />
          <NAMESPACE NAME="virt" />
        </LOCALNAMESPACEPATH>
        <IPARAMVALUE NAME="InstanceName">
          <INSTANCENAME CLASSNAME="HPVM_HostSystem">
            <KEYBINDING NAME="CreationClassName">
              <KEYVALUE VALUETYPE="string">ClassName</KEYVALUE>
            </KEYBINDING>
            <KEYBINDING NAME="Name">
              <KEYVALUE VALUETYPE="string">www.sina.com</KEYVALUE>
            </KEYBINDING>
          </INSTANCENAME>
        </IPARAMVALUE>
      </IMETHODCALL>
    </SIMPLEREQ>
  </MESSAGE>
</CIM>

 

源文件:

#!/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', '');

 

运行结果:

$ ./parser.pl
could not find ParserDetails.ini in /usr/lib/perl5/vendor_perl/5.10/XML/SAX
host.xml: old value is ClassName
host.xml: new value is HostSystem
host.xml: old value is
host.xml: new value is

over~

阅读(2307) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~