[VPM] Comparing XML Files - XML::Simple is your friend

Philip Yuson plyuson at conceptsolutionsbc.com
Wed Dec 8 19:24:59 CST 2004


Thanks for the inputs.
I've finally decided to fix my own script and it is working fine.

One question though.

Attached is the script that extracts an XML file to a Perl 
hash->array->hash->array... object.

The script actually parses two XML files (jrep and ora)
Ran independently, the resulting object is correct.
Ran together, seems like the object in jrep is attached to the object in 
ora.

	use xml_parse;

	my $oxml;
	my $jxml;
	my ($jrep, $orac) = (1, 1);

     if ($jrep) {
	open (JRE, "perl xml_jreport.pl jxml/$ARGV[0]\.txt | ")
		or die  ("Cannot open jreport.pl");
	while (<JRE>) {
	    $jxml .= $_;
	}
	print '*'x50 . "JREPORTS\n";
	print "$jxml\n";
	close (JRE);
	
	my $jp = new xml_parse(debug=> 0);
	my $jbld = $jp->build($jxml);
	print_hash($jbld, 1);
	
	$jp = undef;
	$jbld = undef;
     }

     if ($orac) {
	open (ORA, "perl xml_oracle.pl oxml/$ARGV[0]\.xml | ")
		or die ("Cannot open oracle.pl");
	while (<ORA>) {
	    $oxml .= $_;
	}
	close (ORA);
	print '*'x50 . "ORACLE\n";
	print "$oxml\n";

	my $op = new xml_parse(debug=> 0);
	my $obld = $op->build($oxml);
	print_hash($obld, 1);
	
	$obld = undef;
	$op   = undef;
	
     }
	print "ORACLE OBJECT: " . \%oracle . "\nJREPORT OBJECT: " . \%jrep . "\n";

	sub print_array  {
         	my @array = @{$_[0]};
	        my $count = $_[1];
         	$count++;
	        foreach (@array) {
         	        for (my $x = $count; $x > 1; $x--) {
                 	        print "\t";
	                }
         	        print "Array: $count: $_" . ref($_) . "\n";
                 	if (ref($_) eq 'HASH') {
                         	print_hash($_, $count);
	                }
         	}
	}

	sub print_hash {
         	my %hash = %{$_[0]};
	        my $count = $_[1];
         	$count++;
#		print "$_[0]\n";
	        foreach (sort keys %hash) {
	        	next if ($_ eq 'prev');
         	        for (my $x = $count; $x > 1; $x--) {
                 	        print "\t";
                 	}
	                print "HASH: $count: KEY: $_ => $hash{$_}" . 
ref($hash{$_}) . "\n";
         	        if (ref($hash{$_} ) eq 'ARRAY') {
         	       	        print_array($hash{$_}, $count);
	                } elsif (ref($hash{$_} ) eq 'HASH') {
                 	        print_hash($hash{$_}, $count);
	                }
         	}
	}

If run independently jre and ora have this each:

hash
	array
		hash
		hash
		hash
			array
				hash
				hash
				hash

But run together it shows:

hash
	array
		hash
		hash
		hash
			array
				hash
				hash
				hash
	array
		hash
		hash
		hash
			array
				hash
				hash
				hash
				hash <- other hash is included
					array
						hash
						hash
						hash
							array
								hash
								hash
								hash

Thanks again.

-- 
=======================
Philip L. Yuson
Senior Consultant
Concept Solutions Corporation
Phone: (250) 881-0049
Fax (250) 381-2425



More information about the Victoria-pm mailing list