Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

based on toolics last post How would I extract the XML name and description from the xml below using XML::Twig:
<?xml version="1.0" encoding="utf-8"?> <install type="module" version="1.0.0"> <name>TEST</name> <desription>This is a test</desription> <notebook_tabs> <tab index="1">Create <button internal_name="test1" parent="-1" text="button 1" x_coordinate="5" y_coordinate="5" x_size="-1" y_size="-1" evt_1="EVT_BUTTON" evt_1_function="testclick" /> <button internal_name="test2" parent="-1" text="button 2" x_coordinate="50" y_coordinate="50" x_size="-1" y_size="-1" evt_1="EVT_BUTTON" evt_1_function="testclick2" /> </tab> <tab index="2">Assign</tab> </notebook_tabs> </install>
I've tried using the code below:
use strict; use warnings; use XML::Twig; use Data::Dumper; my $xfile = <<EOF; <?xml version="1.0" encoding="utf-8"?> <install type="module" version="1.0.0"> <name>TEST</name> <desription>This is a test</desription> <notebook_tabs> <tab index="1">Create <button internal_name="test1" parent="-1" text="button 1" x_coordinate="5" y_coordinate="5" x_size="-1" y_size="-1" evt_1="EVT_BUTTON" evt_1_function="testclick" /> <button internal_name="test2" parent="-1" text="button 2" x_coordinate="50" y_coordinate="50" x_size="-1" y_size="-1" evt_1="EVT_BUTTON" evt_1_function="testclick2" /> </tab> <tab index="2">Assign</tab> </notebook_tabs> </install> EOF my $t= new XML::Twig(); $t->parse($xfile); my $install = $t->root(); print "NAME: " . $install->first_child('name') . "\n"; }
but all I get is the entire XML data, what am I doing wrong?

Thanks,

Replies are listed 'Best First'.
Re: One more XML Question
by toolic (Bishop) on Oct 08, 2008 at 19:30 UTC
    XML::Twig is object-oriented. first_child is a method which returns a handle to something. To get the value of the child element, you need to use the text method:
    print "NAME: " . $install->first_child('name')->text() . "\n";

    which prints:

    NAME: TEST

    There is a nice XML::Twig tutorial at http://www.xmltwig.com/xmltwig/, along with a handy reference page of all the methods.

      that makes sense...Thanks!