使用XML::SImple来读写XML
t.xml
<?xml version="1.0" ?><rtsp_execute> <data name="xcoder" tag="abc" ID="001">StreamingMediaTranscoder.exe</data> <data name="src.ip" tag="123" ID="002">10.239.254.202</data> <data name="src.rtsp_port">8554</data> <data name="options">-u SPEED -stats</data> <data>XXXXXXXXXXXXXXXXXXXXXXXXX</data> <param name="kill_at_session_end">1</param> <param name="session_liveness_timeout">0</param></rtsp_execute>
读xml
use strict; use warnings; use XML::Simple; use Data::Dumper; use 5.010; my $xmlfile = "t.xml"; my $xml = XML::Simple->new(KeyAttr=>"",ForceArray => 1); my $tree = $xml->XMlin($xmlfile); say Dumper($tree); my @datas = @{$tree->{"data"}}; my $len = @datas; say "data length = $len"; foreach(@datas){ if(ref($_) eq 'HASH'){ my %itmes = %{$_}; say $itmes{'content'}; }else{ say $_; } }
输出结果
$VAR1 = { 'data' => [ { 'ID' => '001','name' => 'xcoder','content' => 'StreamingMediaTranscoder.exe','tag' => 'abc' },{ 'content' => '10.239.254.202','tag' => '123','ID' => '002','name' => 'src.ip' },{ 'name' => 'src.rtsp_port','content' => '8554' },{ 'content' => '-u SPEED -stats','name' => 'options' },'XXXXXXXXXXXXXXXXXXXXXXXXX' ],'param' => [ { 'name' => 'kill_at_session_end','content' => '1' },{ 'name' => 'session_liveness_timeout','content' => '0' } ] };data length = 5StreamingMediaTranscoder.exe10.239.254.2028554-u SPEED -statsXXXXXXXXXXXXXXXXXXXXXXXXX写xml 总结
以上是内存溢出为你收集整理的Perl 简单读写XML全部内容,希望文章能够帮你解决Perl 简单读写XML所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)