DOM responde a las siglas de "Document Object Model". Proporciona una API para trabajar con documentos XML. GNOME no es sólo un "entorno de trabajo", sino también una API que proporciona funciones y recursos para el programador de aplicaciones para este entorno.
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> <!DOCTYPE menu SYSTEM "menu.dtd"> <menu> <section name="System"> <entry command="Eterm">ETerm, the Enlightened Terminal Emulator</entry> <entry command="gnomecc">GNOME Control Center</entry> </section> <section name="Graphics"> <entry command="gimp">The Gimp</entry> <entry command="gqview">GQView</entry> <entry command="xv">XV</entry> </section> <section name="Sound"> <entry command="xmms">XMMS, X MultiMedia System</entry> <entry command="gqmpeg">GQMpeg</entry> <entry command="gmix">GNOME Mixer</entry> </section> <section name="Editing"> <entry command="gvim">GVim</entry> </section> <section name="Network"> <entry command="netscape">Netscape</entry> <entry command="Eterm -e epic4">Epic4</entry> <entry command="pan">Pan</entry> </section> </menu>
#!/usr/bin/perl -w use XML::DOM; use Gnome; my $parser = new XML::DOM::Parser; my $doc = $parser->parsefile ("menu-perl-gtk.xml"); my $nodes = $doc->getElementsByTagName ("section"); my $num_of_sections = $nodes->getLength; init Gnome "ejemplo-xml.pl"; $factory = new Gtk::MenuFactory('menu_bar'); $subfactory = new Gtk::MenuFactory('menu_bar'); $factory->add_subfactory($subfactory, '<Main>'); ### Menú principal: my $tmpitem = { path => "<Main>/Main/About", callback => sub { AboutBox; } }; $factory->add_entries($tmpitem); $tmpitem = { path => "<Main>/Main/Quit", callback => sub { Gtk->exit(0); } }; $factory->add_entries($tmpitem); # Secciones: my %sections; for (my $i=0; $i<$num_of_sections; $i++) { my $node = $nodes->item($i); # Cada $node es una sección my $section = $node->getAttribute("name"); my $entries = $node->getChildNodes; # Lista de entradas # en el menú # (sección) my $num_of_entries = $entries->getLength; # Núm. de entradas en # el menú for (my $j=0; $j<$num_of_entries; $j++) { my $entry = $entries->item($j); my $item; if ($entry->getNodeName eq "entry") { @entry_data = $entry->getChildNodes; $text = $entry_data[0]->getData; my $command = $entry->getAttribute("command"); $item = { path => "<Main>/$section/$text", callback => sub{system "$command"} }; } $factory->add_entries($item); # Añado el item } } ### Ahora viene la parte de la representación con GTK: $menubar = $subfactory->widget; $menubar->show; $win = new Gtk::Window; $win->add($menubar); $win->show; $win->signal_connect('delete_event', sub {Gtk->exit(0)}); main Gtk; sub AboutBox { my $about = new Gnome::About "Ejemplo de libXML-DOM-Perl", "0.01", "GPLed software", ["robe@gpul-org"], "Un ejemplo de cómo usar DOM desde Perl"; $about->signal_connect(destroy => sub { return; }); show $about; }