in reply to Trees in XML

You may find XML::TreeBuilder or XML::Twig are more appropriate for your application. Consider:

use strict; use warnings; use XML::TreeBuilder; my $xml = <<XML; <?xml version='1.0' encoding='UTF-8'?> <list name="name list"> <person> <firstname>Paul</firstname> <lastname>Rutter</lastname> <age>24</age> </person> <person> <firstname>Ruth</firstname> <lastname>Brewster</lastname> <age>22</age> </person> <person> <firstname>Cas</firstname> <lastname>Creer</lastname> <age>23</age> </person> </list> XML my $root = XML::TreeBuilder->new (); $root->parse ($xml); my @firstNames = map {$_->as_text ()} $root->look_down (_tag => 'first +name'); print "TreeBuilder: @firstNames\n"; use XML::Twig; my $twig = XML::Twig->new (twig_roots => { 'person/firstname' => \&p +ushName}); @firstNames = (); $twig->parse ($xml); print "Twig: @firstNames\n"; sub pushName { my ($t, $elt) = @_; push @firstNames, $elt->text (); }

Prints:

TreeBuilder: Paul Ruth Cas Twig: Paul Ruth Cas

Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^2: Trees in XML
by Anonymous Monk on Jun 03, 2008 at 11:08 UTC
    I am aware of other modules out there but the literature I have read suggests that XML::Parser is best suited to me. I am keen to stick to using this module.

    I also do not manage the computer I work on and so installing extra modules is not simple and very time consuming. I would rather stick to XML::Parser. Any help on how to access the data out of the array?
    Would it be like a normal array? In which case I could use a while loop and $data[0] references?

    any help would be super

      TreeBuilder and Twig are pure-perl modules, which use XML::Parser for their work.