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

Perl XML::Simple parse simple xml file

发布时间:2020-12-15 20:50:00 所属栏目:大数据 来源:网络整理
导读:用Perl解析xml文件,又不想下载其它模块如Dom,可以用XML::Simple来完成这个任务。 实际工作中用到的几个有用的属性: 1. KeepRoot = 1 生成的hash中显示根节点,否则从下一层节点开始显示。 2. ForceContent = 1 生成的hash中以hash结构保存节点值,如: no

用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' ????????????????????????? } ??????????????????????? ] ??????? };

(编辑:李大同)

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

    推荐文章
      热点阅读