Question regarding XML::Twig and parsing subelements of an XML-Tree

Hi everybody,

this XML-thing ist currently driving me nuts :) let's assume i have the following
XML-Tree as input:

# ------------------------- file.xml --------------------------- #
<MOVIE_LIST>
<MOVIE>
<NAME>Name of the Movie</NAME>
<MOVIE_ID>28372382</MOVIE_ID>
<DESCRIPTIONS>
<LONG_DESCRIPTION>This is a long description</LONG_DESCRIPTION>
<SHORT_DESCRIPTION>short description</SHORT_DESCRIPTION>
</DESCRIPTIONS>
<DIRECTOR_LIST>
<DIRECTOR>director 1</DIRECTOR>
<DIRECTOR>director 2</DIRECTOR>
</DIRECTOR_LIST>
</MOVIE>
<MOVIE>
....
</MOVIE>
</MOVIE_LIST>

# ------------------------------------------------------------ -- #

I'm using the Perl-module "XML::Twig" for parsing the input-file (the input-xml-file
is round about 200MB - so it's quite big and XML::Twig is really efficient with
memory-usage).

Extracting values from the XML-file that do not contain childs (like NAME or MOVIE_ID)
is working like a charm but so far i didn't manage to extract the child-objects
successfully (Like the DIRECTOR in DIRECTOR_LIST).

I'm currently working with this snipplet:

# ------------------------- snip --------------------------- #
#!/usr/bin/perl -w
use strict;
use warnings;
use XML::Twig;
use Data::Dumper;

my $file = 'file.xml';
my $twig = new XML::Twig(
TwigHandlers => { 'MOVIE' => \&movie },
TwigRoots => {MOVIE_LIST => 1},
pretty_print => 'indented');
$twig->parsefile($file);

sub movie {
my ($t, $elt) = [at] _;

my $MOVIE_ID = "";
my $NAME = "";

# this is working pretty well
$MOVIE_ID = $elt->first_child_text('FILM_ID');
$NAME = $elt->first_child_text('NAME');

push( [at] movies, [ $MOVIE_ID,
$NAME,
]);

# reduce memory usage :-)
$t->flush;
}

print Dumper \ [at] movies;

# ------------------------- /snip --------------------------- #

So, how do i extract the child values within the <DIRECTOR_LIST> ?
I didn't manage so far, so any hints are very appreciated.

Thanks,
Werner

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
werner [ Sa, 16 April 2011 15:02 ] [ ID #2058237 ]

Re: Question regarding XML::Twig and parsing subelements of an XML-Tree

Hi,


# something like the following also doesn't work :(
[at] directors = $elt->first_child('DIRECTOR_LIST')->children('DIRECTOR');

i get the following error:
"Can't call method "first_child" on an undefined value at test.pl line 53."

regards,
Werner

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
werner [ Sa, 16 April 2011 15:42 ] [ ID #2058238 ]

Re: Question regarding XML::Twig and parsing subelements of an XML-Tree

Hi,

that did the trick:

[at] directors = $elt->first_child('DIRECTOR_LIST')->children_text('DIRECTOR' );

now, every director is listet in [at] directors-array =)

Bye,
Werner

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
werner [ Sa, 16 April 2011 16:01 ] [ ID #2058239 ]
Perl » gmane.comp.lang.perl.beginners » Question regarding XML::Twig and parsing subelements of an XML-Tree

Vorheriges Thema: Padre IDE
Nächstes Thema: hash troubles