in reply to XML Parsing questions and recommendations?
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> <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(); my $note_tabs = $install->first_child('notebook_tabs'); my @tabs = $note_tabs->children('tab'); for my $tab (@tabs) { print "\nTab ", $tab->att('index'), "\n"; my @buttons = $tab->children('button'); print "number of buttons = ", scalar @buttons, "\n"; for my $button (@buttons) { my @params = $button->atts(); print Dumper(\@params); } }
Prints:
Tab 1 number of buttons = 2 $VAR1 = [ { 'parent' => '-1', 'y_coordinate' => '5', 'x_coordinate' => '5', 'evt_1_function' => 'testclick', 'x_size' => '-1', 'evt_1' => 'EVT_BUTTON', 'internal_name' => 'test1', 'text' => 'button 1', 'y_size' => '-1' } ]; $VAR1 = [ { 'parent' => '-1', 'y_coordinate' => '50', 'x_coordinate' => '50', 'evt_1_function' => 'testclick2', 'x_size' => '-1', 'evt_1' => 'EVT_BUTTON', 'internal_name' => 'test2', 'text' => 'button 2', 'y_size' => '-1' } ]; Tab 2 number of buttons = 0
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML Parsing questions and recommendations?
by Anonymous Monk on Oct 08, 2008 at 18:13 UTC | |
by Anonymous Monk on Oct 08, 2008 at 18:23 UTC |