用Perl解析xml文件,又不想下载其它模块如Dom,可以用XML::Simple来完成这个任务。
实际工作中用到的几个有用的属性:
1. KeepRoot => 1
生成的hash中显示根节点,否则从下一层节点开始显示。
2. ForceContent => 1
生成的hash中以hash结构保存节点值,如:
nodename => { content => 'value' }
否则显示为:
nodename => 'value'
3. ForceArray => ['nodename1','nodename2'] 所有以此开始的节点都用array ref的形式表示,在不使用这个属性的时候,如果是单个节点,则以hash ref的形式表示。如: <node> <test>this is test</test> </node> 使用属性: node => [ test => 'this is test' ] 不使用属性: node => { test => 'this is test' } 其它属性可以参考Perldoc文档。 如下是一个例子,使用以上所有属性。 -------------------------------------------------------------------------------- use Data::Dumper; use XML::Simple; my $dict = <<END; <security> <itemaction action='delete'> <test1 test="abc" asf='asdfa'>testing12</test1> <abc> <test2 test="abc">testing13</test2> <test3 test="abc">testing14</test3> </abc> </itemaction> </security> END my $x = XMLin($dict,ForceContent => 1,ForceArray => ['itemaction']); print Dumper $x 输出: -------------------------------------------------------------------------------- $VAR1 = { 'itemaction' => [ { 'abc' => { 'test3' => { 'test' => 'abc', 'content' => 'testing14' }, 'test2' => { 'test' => 'abc', 'content' => 'testing13' } }, 'test1' => { 'test' => 'abc', 'asf' => 'asdfa', 'content' => 'testing12' }, 'action' => 'delete' } ] };
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。