perl 讀寫XML

來源:互聯網
上載者:User

標籤:

perl 讀寫xml:主要是寫總是忘記怎麼搞。。。

一 讀xml

1)xml執行個體
<?xml version="1.0" encoding="UTF-8"?>
<employees> 
  <employee age = "30">
    <name>linux</name>
    <country>US</country>
  </employee>  
  <employee age = "10">
    <name>mac</name>
    <country>US</country>
  </employee>
  <employee age = "20"> 
    <name>windows</name>
    <country>US</country>
  </employee>
</employees>
 
2)代碼
use File::Basename;
use XML::Simple;
use Data::Dumper;


my $xmlfile = dirname($0) . "\\employees.xml";
if (-e $xmlfile)
{
    print "-----------------------------------------\n";
    my $userxs = XML::Simple->new(KeyAttr => "name");
    my $userxml = $userxs->XMLin($xmlfile);
    # print output
    print Dumper($userxml);
    
    my %allemployees = %{$userxml->{employee}};
    foreach my $key (keys(%allemployees))
    {
      print $key . " ";
      print $allemployees{$key}{"age"} . "\n";
    }


    print "-----------------------------------------\n";
       my $userxsT = XML::Simple->new(ForceArray => 1);
    my $userxmlT = $userxsT->XMLin($xmlfile);
    # print output
    print Dumper($userxmlT); 
    
    my @allemployeeT = @{$userxmlT->{"employee"}};
    foreach my $employee (@allemployeeT)
    {
      print $employee->{"name"}[0] . " ";
      print $employee->{"age"} . "\n";
    }    
}
 
3)結果


 
二 寫xml
代碼:
use File::Basename;
use XML::Simple;
use Data::Dumper;


print "-----------------------------------------\n";
# create array
my @arr = [ 
        {‘country‘=>‘england‘, ‘capital‘=>‘london‘},
        {‘country‘=>‘norway‘, ‘capital‘=>‘oslo‘},
        {‘country‘=>‘india‘, ‘capital‘=>‘new delhi‘} ];


# create object
my $xml = new XML::Simple(NoAttr=>1, RootName=>‘dataroot‘);


# convert Perl array ref into XML document 
my $data = $xml->XMLout(\@arr ,outputfile => "output1.xml");




print "-----------------------------------------\n";
my $str=<<_XML_STRING_;
<config logdir="/var/log/foo/" debugfile="/tmp/foo.debug">
<server name="sahara" osname="solaris" osversion="2.6">
  <address>10.0.0.101</address>
  <address>10.0.1.101</address>
</server>
<server name="gobi" osname="irix" osversion="6.5">
  <address>10.0.0.102</address>
</server>
<server name="kalahari" osname="linux" osversion="2.0.34">
  <address>10.0.0.103</address>
  <address>10.0.1.103</address>
</server>
</config>
_XML_STRING_


my $xml_ref=XMLin($str,KeepRoot => 1);
my $xml_str=XMLout($xml_ref,outputfile => "output2.xml");
print "-----------------------------------------\n";
 
 
三 更多
常用參數:
KeyAttr => [ list ] # in+out - important or KeyAttr => { list } # in+out - important  表示讀取出來的dict中的key。
ForceArray => 1 # in - important or ForceArray => [ names ] # in - important  表示dict中的子項目表示為array,而不是dict。
NoAttr => 1 # in+out - handy 表示是否將子項目和屬性都寫為屬性,讀入時是否忽略屬性。
RootName => ‘string‘ # out - handy 表示寫xml時的root節點名字。
KeepRoot => 1 # in+out - handy 表示是處理取root節點
 
xml-simple模組詳細參考:http://search.cpan.org/~grantm/XML-Simple-2.18/lib/XML/Simple.pm
 
完!
感謝,Thanks!

perl 讀寫XML

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.