in reply to Get Node Value from irregular XML
Produces the following (After adding a proper closing </root> tag to the second example):#!/bin/perl -w use strict; use XML::Twig; my $twig= new XML::Twig( twig_handlers => { _all_ => \&Piece_handler , # Call for each 'Program' +elemetn } ); $twig->parsefile( shift @ARGV ); # build the twig #------------------------------------------------- sub Piece_handler{ my( $twig, $p)= @_; # handlers params are alwa +ys # the twig and the el +ement return unless $p->text && $p->text=~/design/; print $p->tag , ":",$p->text,"\n"; #Traverse and print ancestor tags for (my $ancestor = $p->parent; defined $ancestor; $ancestor=$anc +estor->parent()){ print "\tParent : ", $ancestor->tag, "\n"; } $p->cut; # This one is processed, delete from tree .. }
$ perl xmlt.pl xmldata2.xml para:This is a design XZY document for Project Parent : root $ perl xmlt.pl xmldata.xml header: This is a design XZY document for Project Parent : sect Parent : part Parent : root
"The trouble with the Internet is that it's replacing masturbation as a leisure activity."
-- Patrick Murray
|
|---|