eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}' if 0; use XML::DOM; my $str = < ]> SPHN IBM Blue Horizon at SDSC 920 of 1152 processors busy. END my $parser = new XML::DOM::Parser (ParseParamEnt => 1, KeepCDATA => 1); my $node = $parser->parse ($str); doItFirst ( $node ); while ( 1 ) { print "\n-------------------------------------------------------------\n"; print " d: \"down\" -- u: \"up\" -- r: \"right\" -- l: \"left\"\n"; print " -- x: \"xml\" -- q: \"quit\"\n"; print "-------------------------------------------------------------\n"; read STDIN, $val, 2; chop $val; if ( $val eq "d" ) { if ( getNodeType( $node->getNodeType ) == 9 ) { $node = $node->getDocumentElement; doItFirst ( $node ); } else { $node = $node->getFirstChild; my $nodeN = $node->getNextSibling; while ($node->getNodeType == 3 && ($nodeN != "")) { $node = $node->getNextSibling; } doItFirst ( $node ); } } elsif ( $val eq "u" ) { $node = $node->getParentNode; doItFirst ( $node ); } elsif ( $val eq "r" ) { $node = $node->getNextSibling; while ($node->getNodeType == 3) { $node = $node->getNextSibling; } doItFirst ( $node ); } elsif ( $val eq "l" ) { $node = $node->getPreviousSibling; while ($node->getNodeType == 3) { $node = $node->getPreviousSibling; } doItFirst ( $node ); } elsif ( $val eq "x" ) { print "\t\tXML CONTENT:\n"; print $node->toString; print "\n"; } elsif ( $val eq "q" ) { goto END; } else { } } END:; sub doItFirst { my ($node) = @_; # Current Node Information print "\t--> ", $node->getNodeName, " Type:", getNodeType( $node->getNodeType), "\n"; printNodeInfo ( $node, $node->getNodeType ); if ( $node->hasChildNodes ) { my @nodes = $node->getChildNodes; my $count = $#nodes + 1; print "\n\t\t *** $count CHILDREN ARE:\n"; print "\t\t -----------------\n"; for my $kid (@nodes) { if ( $kid->getNodeType != 3 || ($kid->getNodeType == 3 && $count == 1 ) ) { print "\t\t - ", $kid->getNodeName, " Type:", getNodeType( $kid->getNodeType), "\n"; } } } else { print "\n\t\t *** NO CHILDREN:\n"; print "\t\t ----------------\n"; } } sub getNodeType { my ($intType) = @_; if ( $intType == 1 ) { return "ELEMENT (1)"; } elsif ( $intType == 2 ) { return "ATTRIBUTE (2)"; } elsif ( $intType == 3 ) { return "TEXT (3)"; } elsif ( $intType == 4 ) { return "CDATA SECTION (4)"; } elsif ( $intType == 5 ) { return "ENTITY REFERENCE (5)"; } elsif ( $intType == 6 ) { return "ENTITY (6)"; } elsif ( $intType == 7 ) { return "PROCESSING INSTRUCTION (7)"; } elsif ( $intType == 8 ) { return "COMMENT (8)"; } elsif ( $intType == 9 ) { return "DOCUMENT (9)"; } elsif ( $intType == 10 ) { return "DOCUMENT TYPE (10)"; } elsif ( $intType == 11 ) { return "DOCUMENT FRAGMENT (11)"; } elsif ( $intType == 12 ) { return "NOTATION (12)"; } elsif ( $intType == 13 ) { return "ELEMENT DECLARATION (13) -- not part of DOM"; } elsif ( $intType == 14 ) { return "ATTRIBUTE DECLARATION (14) -- not part of DOM"; } elsif ( $intType == 15 ) { return "XML DECLARATION (15) -- not part of DOM"; } elsif ( $intType == 16 ) { return "ATTRIBUTE LIST DECLARATION (16) -- not part of DOM"; } } sub printNodeInfo { my ($node, $intType) = @_; #"ELEMENT (1)"; if ( $intType == 1 ) { my $map = $node->getAttributes; my $list = $map->getValues; # print "len: ", $list->getLength, "\n"; for my $val ( @$list ) { print "\t\t\t", $val->getName, "\t ", "Type:", getNodeType ( $val->getNodeType ), " -- value= ", $val->getNodeValue, "\n"; } } #"ATTRIBUTE (2)"; elsif ( $intType == 2 ) { } #"TEXT (3)"; elsif ( $intType == 3 ) { print "\t\t -- value= ", $node->getNodeValue, "\n"; } #"CDATA SECTION (4)"; elsif ( $intType == 4 ) { } #"ENTITY REFERENCE (5)"; elsif ( $intType == 5 ) { } #"ENTITY (6)"; elsif ( $intType == 6 ) { } #"PROCESSING INSTRUCTION (7)"; elsif ( $intType == 7 ) { } #"COMMENT (8)"; elsif ( $intType == 8 ) { print "\t\t -- value= ", $node->getNodeValue, "\n"; } #"DOCUMENT (9)"; elsif ( $intType == 9 ) { my $xmlobj = $node->getXMLDecl; print "\t\t - xml Type:", getNodeType( 15 ), "\n"; print "\t\t\t version Type:", getNodeType( 2 ), " -- value= ", $xmlobj->getVersion, "\n"; } #"DOCUMENT TYPE (10)"; elsif ( $intType == 10 ) { return "DOCUMENT TYPE (10)"; } #"DOCUMENT FRAGMENT (11)"; elsif ( $intType == 11 ) { } #"NOTATION (12)"; elsif ( $intType == 12 ) { } #"ELEMENT DECLARATION (13) -- not part of DOM"; elsif ( $intType == 13 ) { } #"ATTRIBUTE DECLARATION (14) -- not part of DOM"; elsif ( $intType == 14 ) { } #"XML DECLARATION (15) -- not part of DOM"; elsif ( $intType == 15 ) { } #"ATTRIBUTE LIST DECLARATION (16) -- not part of DOM"; elsif ( $intType == 16 ) { } }