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

如何使用XML :: Twig获取内容?

发布时间:2020-12-16 23:50:43 所属栏目:百科 来源:网络整理
导读:我的目标是start_tag_handler(见下文)在找到应用程序/标题标签时获取应用程序/标题内容(请参阅下面的示例 XML). 和 end_tag_handler在找到apps / logs标记时获取应用程序/日志内容. 但相反,此代码返回null并退出. 这是用于解析的Perl代码(使用XML::Twig)###
我的目标是start_tag_handler(见下文)在找到应用程序/标题标签时获取应用程序/标题内容(请参阅下面的示例 XML).


end_tag_handler在找到apps / logs标记时获取应用程序/日志内容.

但相反,此代码返回null并退出.

这是用于解析的Perl代码(使用XML::Twig)###:

#!/usr/local/bin/perl -w

    use XML::Twig;
    my $twig = XML::Twig->new(
                start_tag_handlers =>
                  { 'apps/title' => &;kicks
                  },twig_roots =>
                  { 'apps' => &;app
                  },end_tag_handlers =>
                  { 'apps/logs' => &;bye
                  }
                );
    $twig -> parsefile( "doc.xml");

    sub kicks {
        my ($twig,$elt) = @_;
        print "---kicks--- n";
        print $elt -> text;
        print " n";
    }

    sub app {
        my ($twig,$apps) = @_;
        print "---app--- n";
        print $apps -> text;
        print " n";
    }


    sub bye {
        my ($twig,$elt) = @_;
        print "bye n";
        print $elt->text;
        print " n";
    }

这是doc.xml ###:

<?xml version="1.0" encoding="UTF-8"?>
    <auto>
      <apps>
        <title>watch</title>
        <commands>set,start,00:00,alart,end</commands>
        <logs>csv</logs>
      </apps>
      <apps>
        <title>machine</title>
        <commands>down,select,vol_100,check,line,end</commands>
        <logs>dump</logs>
      </apps>
    </auto>

这是控制台###中的输出:

C:&;perl parse.pl
    ---kicks---

    ---app---
    watchset,endcsv
    ---kicks---

    ---app---
    machinedown,enddump
查看 start_tag_handlers的XML :: Twig文档:

The handlers are called with 2 params: the twig and the element. The element is empty at that point,its attributes are created though.

在调用start_tag_handlers时,甚至还没有看到文本内容,因为解析开始标记(例如< title>,而不是结束标记< / title>)刚刚完成.

end_tag_handlers不提供元素文本的原因可能是对称:-).

您想要的可能是使用twig_handlers:

my $twig = XML::Twig->new(
    twig_handlers => {
        'apps/title' => &;kicks,'apps/logs' => &;bye
    },twig_roots => {
        'apps' => &;app
    },);

输出是:

---kicks--- 
watch 
bye 
csv 
---app--- 
watchset,endcsv
---kicks--- 
machine 
bye 
dump 
---app--- 
machinedown,enddump

(编辑:李大同)

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

    推荐文章
      热点阅读