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

时间:2022-09-21 21:11:00

My aim is that start_tag_handler (see below) get the apps/title content when it finds an apps/title tag (see sample XML below).

我的目标是start_tag_handler(见下文)在发现应用程序/标题标记时获取应用程序/标题内容(参见下面的示例XML)。

And end_tag_handler gets the apps/logs content when it finds an apps/logs tag.

end_tag_handler在找到apps/logs标签时获取apps/logs内容。

But instead this code returns null and exits.

但相反,这段代码返回null并退出。

This is the Perl code for parsing (using 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";
    }

This is 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>

This is the output in the console###:

    C:\>perl parse.pl
    ---kicks---

    ---app---
    watchset,start,00:00,alart,endcsv
    ---kicks---

    ---app---
    machinedown,select,vol_100,check,line,enddump

1 个解决方案

#1


9  

Check out the XML::Twig documentation for start_tag_handlers:

查看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.

处理程序使用两个参数调用:twig和元素。元素在那个点是空的,但是它的属性是创建的。

At the time start_tag_handlers is called, the text content isn't even seen yet, since parsing of the start tag (e.g. <title>, not the end tag </title>) has only just completed.

在调用start_tag_handlers时,甚至还没有看到文本内容,因为对开始标记(例如,

,而不是结束标记)的解析才刚刚完成。

The reason that end_tag_handlers don't supply element text is probably for symmetry :-).

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

What you want is probably to use twig_handlers instead:

你想要的可能是使用twig_handlers:

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

Output is:

输出是:

---kicks--- 
watch 
bye 
csv 
---app--- 
watchset,start,00:00,alart,endcsv
---kicks--- 
machine 
bye 
dump 
---app--- 
machinedown,select,vol_100,check,line,enddump

#1


9  

Check out the XML::Twig documentation for start_tag_handlers:

查看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.

处理程序使用两个参数调用:twig和元素。元素在那个点是空的,但是它的属性是创建的。

At the time start_tag_handlers is called, the text content isn't even seen yet, since parsing of the start tag (e.g. <title>, not the end tag </title>) has only just completed.

在调用start_tag_handlers时,甚至还没有看到文本内容,因为对开始标记(例如,

,而不是结束标记)的解析才刚刚完成。

The reason that end_tag_handlers don't supply element text is probably for symmetry :-).

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

What you want is probably to use twig_handlers instead:

你想要的可能是使用twig_handlers:

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

Output is:

输出是:

---kicks--- 
watch 
bye 
csv 
---app--- 
watchset,start,00:00,alart,endcsv
---kicks--- 
machine 
bye 
dump 
---app--- 
machinedown,select,vol_100,check,line,enddump