How to compare two XML files in perl?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vibhakhushi
    New Member
    • Mar 2010
    • 33

    How to compare two XML files in perl?

    How to compare two files in perl. I have two files as shown below.

    Code:
    First XML File
    <Data>
    <indep voltage>
      +1.20000000000e+01
    </indep>
    <indep current>
      +5.08474576271e-04
    </indep>
    </Data>
    
    Second XML File:
    <Data>
    <indep voltage>
      +1.20000000000e+01
    </indep>
    <indep current>
      +5.08474576271e-04
    </indep>
    </Data>
    I just want to confirm whether Data,indep tags are present with the specified attributes current and voltage.
    I don't want whether the values are same.
    I am confused to use the modules like XML::Simple and all.
    Pls suggest me how to do that. I am newbie in perl. Pls provide me the good example also so that i can understand it.
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    To compare two XML documents and find the differences, you may make use of XML::SemanticDi ff.

    The documentation given in CPAN would be sufficient to start with the script. Try it out and if you face any problem, post it here.

    Comment

    • vibhakhushi
      New Member
      • Mar 2010
      • 33

      #3
      Hello nithinpes, I am using XML::SemanticCo mpare to compare two files. I downloaded that module. But I'm getting the error
      can't use an undefined value as an ARRAY reference at C:/Perl/site/lib/XML/SemanticCompare . I didn't get what it means. Can you put some light on that?
      My Code is:
      Code:
      	$file1 = $args[0] . "\\test.xml";
      	$file2 = $args[0] . "\\test1.xml";
       
      	local $/;
      	open(my $in, "<$file1") or die "File1: $!\n";
      	my $xml1=<$in>;
      	close($in);
      	open($in, "<$file2") or die "File2: $!\n";
      	my $xml2=<$in>;
      	close($in);
       
       	#open(my $out, ">output.txt") or die "Output: $!\n";
      	# get the diffs
      	my $diffs_arrayref = $x->diff($control_xml, $test_xml);
      	print @$diffs_arrayref;
      	#close($out);

      Comment

      • vibhakhushi
        New Member
        • Mar 2010
        • 33

        #4
        Hello nithinpes. I rectified the error. But I don't know how to interpret the output of XML::SemanticCo mpare->diff() method. Pls put some light on that.

        Comment

        Working...