加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl使用xml::simple来读写xml

发布时间:2020-12-16 00:16:35 所属栏目:大数据 来源:网络整理
导读:? 一 读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 ??

?

一 读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

    ?

    完!

  • (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读