#!/usr/bin/perl -w use strict; use XML::DOM; use Data::Dumper; my $parser = new XML::DOM::Parser; my $doc = $parser->parsefile ("samplexml.xml"); my $root = $doc->getDocumentElement(); ########################################################### sub traverse { my($node)= @_; if ($node->getNodeType == ELEMENT_NODE) { print Dumper (get_children_hash_by_node($node)); my $rot_hash = get_children_hash_by_node($node); if ($rot_hash) { foreach my $child ($rot_hash->{children}) { traverse($child); } } } } ########################################################### ########################################################### sub get_children_hash_by_node($) { my ($node)=@_; my $this_node; my $att_hash; my @children; my @children_names, my $node_data; if ($node->getNodeType == ELEMENT_NODE) { my $textNode = $node->getFirstChild(); if ($textNode) { $node_data = $textNode->getNodeValue(); } $this_node = $node->getNodeName; my $atts = $node->getAttributes(); #print $this_node."\n"; my $thisItem = 0; while ($atts->item($thisItem)){ $att_hash->{$atts->item($thisItem)->getNodeName() +}->{value} = $atts->item($thisItem)->getNodeValue(); $thisItem++; } foreach my $child ($node->getChildNodes()) { if ($child->getNodeType == ELEMENT_NODE){ push (@children, $child); push (@children_names, $child->getNodeName()); } } } my $ret_node = { name => $this_node, attributes => $att_hash, value => $node_data, children_names => \@children_names, children => \@children, }; return $ret_node; } ########################################################### ########################################################### sub get_x_info($ $ $) { my ($xdoc, $xpath, $data)=@_; #get_x_info(('node_name|1','child_node|0','child_node|0') , 'ALL') +; #'ALL' returns hash with all of the following #'ATTS' returns attributes hash #'VALUE' returns value of the node #'NAME' returns node name as string #'CHILD_NAMES' returns an array of strings #'CHILDREN' returns an array of hashes #'EXISTS' my $ret_val; my $iter = 0; my ($xnode, $i) = split (/\|/,@{$xpath}[0]); if (!$i){$i=0;} shift(@{$xpath}); my $rot_node = get_children_hash_by_node($xdoc); if ($xnode eq $rot_node->{name}) { if (@{$xpath}[0]) { ($xnode, $i) = split (/\|/,@{$xpath}[0]); if (!$i){$i=0;} foreach my $child ( @{$rot_node->{children}} ) { if ($child and ($child->getNodeName() eq $xnode) and $ +iter != $i) { $iter++; } elsif ($child and ($child->getNodeName() eq $xnode) an +d $iter == $i) { $ret_val = &get_x_info($child, \@{$xpath}, $data); last; } else { $ret_val = ($xnode." node does not exist"); } } } else { if ($data eq 'ALL') { $ret_val = $rot_node; } elsif ($data eq 'ATTS') { $ret_val = $rot_node->{attributes}; } elsif ($data eq 'VALUE') { $ret_val = $rot_node->{value}; } elsif ($data eq 'NAME') { $ret_val = $rot_node->{name}; } elsif ($data eq 'CHILD_NAMES') { $ret_val = $rot_node->{children_names}; } elsif ($data eq 'CHILDREN') { $ret_val = $rot_node->{children}; } elsif ($data eq 'NODE') { $ret_val = $xdoc; } elsif ($data eq 'EXISTS') { $ret_val=""; } } } return $ret_val; } ########################################################### #print "\n-----------------------------------------------------\n"; #traverse($root); #print Dumper (get_x_info($root,['SOAP-ENV:Envelope|0','SOAP-ENV:Heade +r|0'],'CHILD_NAMES')); #print Dumper (get_children_hash_by_node($root)); print Dumper (get_x_info($root, [ 'SOAP-ENV:Envelope', 'SOAP-ENV:Body', 'SOAPSDK1:AdminRequestResponse', 'Result', 'getTransactionResponse', 'transactions', 'transaction|3', 'referenceNumber' ], 'NODE'));