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

前端之家收集整理的这篇文章主要介绍了如何使用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
原文链接:https://www.f2er.com/xml/292107.html

猜你在找的XML相关文章