use warnings; use strict; use XML::TreeBuilder; my $str = <<'STR'; attr 1 attr 2 attr 1 attr 3 STR my $xml = XML::TreeBuilder->new; $xml->parse($str); my @attributes = $xml->find('attribute'); for (@attributes) { # Figure out where the element is my @lineage = reverse $_->lineage (); my @eNames = map {$_->tag() . ($_->attr('name') ? '(' . $_->attr('name') . ')' : '')} @lineage; print join ('/', @eNames), ":\n"; # Access the element text my $oldText = $_->as_text(); print "$oldText\n"; # Alter the content $_->push_content ("Extra attribute added"); } print $xml->as_HTML ('<>&', ' '); # Looks bogus, but gets indentation