use strict; use XML::LibXML; my $file; $file = 'myHash2Xml.xml'; ## load the XML my $parser = XML::LibXML->new(); my $tree = $parser->parse_file($file); my $root = $tree->getDocumentElement; ## get all the elements that exist in a doc. Note the wildcard to search through everything. my @allDocElements = $root->getElementsByTagName('*'); ## some stats meters my $useableElements = 0; my $useableAtts = 0; my $count = @allDocElements; ## iterate over all the elements foreach my $el1 (@allDocElements) { ## if we have no child nodes we are at the bottom of the tree. This is what we ## will be looking for most of the time. if ( !$el1->hasChildNodes() ) { if ( $el1->hasAttributes() ) { ## iterate over all the attributes $useableElements++; foreach my $ttt ( $el1->attributes() ) { $useableAtts++; my $string = $el1->nodePath() . "/" . $ttt->localName . " = " . $ttt->nodeValue; print "AT - $string\n"; } } else { ## haven't hit this yet $useableElements++; my $string = $el1->nodePath() . "/--" . $el1->localName . " = " . $el1->nodeValue; print "EL - $string\n"; } } else { ## if we are not at the bottom of the tree, we could still have attributes. Check here ## if we do and process as above if ( $el1->hasAttributes() ) { $useableElements++; foreach my $ttt ( $el1->attributes() ) { $useableAtts++; my $string = $el1->nodePath() . "/" . $ttt->localName . " = " . $ttt->nodeValue; print "AT - $string\n"; } } else { ## keep an eye out for text added to elements if ( $el1->textContent() !~ /\n/ ) { $useableElements++; my $string = $el1->nodePath() . " = " . $el1->textContent; print "TX - $string\n"; } } } } print "\nFrom $count we found $useableElements useable elements, with $useableAtts attributes\n";