Quantcast
Channel: How can I get content using XML::Twig? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How can I get content using XML::Twig?

$
0
0

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).

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

But instead this code returns null and exits.

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

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>