For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > December 2007 > XML::Twig question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author XML::Twig question
goldtech

2007-12-23, 7:00 pm

Hi,

Trying this but only "aaa" outputs not "bbb". What's fix? I'm new to
XML and Perl - help appreciated.

xml file:

<?xml version='1.0'?>
<config>
<attrbs>
<attrb>aaa</attrb>
<attrb>bbb</attrb>
</attrbs>
</config>


perl file:

#!/usr/bin/perl -w
use strict;
use XML::Twig;

my $twig= XML::Twig->new( twig_roots => {'/config/attrbs' =>
\&do_attrb});
$twig->parsefile( "xmlt1.xml");
$twig->purge();

sub do_attrb {
my($a1) = $_->find_nodes("attrb");
print $a1->text, "\n";
}


output:

aaa

mirod

2007-12-24, 7:59 am

On Dec 23, 2:25 pm, goldtech <goldt...@worldpost.com> wrote:

> my($a1) = $_->find_nodes("attrb");
> print $a1->text, "\n";


find_nodes returns a list of nodes, so the first line here
assigns the first element of that list to $a1.

I think you want
my @attrb= _->find_nodes("attrb");
print map { $_->text } @attrb;

Does that help?

--
mirod
goldtech

2007-12-24, 7:01 pm

....snip...
>
> I think you want
> my @attrb= $_->find_nodes("attrb");
> print map { $_->text } @attrb;
>
> Does that help?
>


Perfect - Thank you.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com