satzbu has asked for the wisdom of the Perl Monks concerning the following question:

i have two xml files like this
1. <xml> <chapter>sathish <subchap1>sathish is good boy or not <p>sathish is a good bou</p> </subchap1> </chapter> </xml> 2.<xml> <chapter>head <subchap1>body <p>para</p> </subchap1> </chapter> </xml>
this two xml files are my inputs i want the output as
<xml> <head>sathish <body>sathish is good boy or not <para>sathish is a good bou</para> <//body> </head> </xml> </code. i mean the second xml file contents are replaced to the first xml file + tags please help any one how can i do this i use the code as <code> #!/usr/bin/perl -w use warnings; use strict; use XML::Simple; use Data::Dumper; my %hash; my %hash1; my $file= $ARGV[0]; my $simple = XML::Simple->new(); my $hash = $simple->XMLin($file); my $file1= $ARGV[1]; my $hash1 = $simple->XMLin($file1); print Dumper ($hash); print Dumper ($hash1); my $areEqual=1; if(keys %hash == keys %hash1) { foreach my $key(keys %hash) { if(!exists $hash1{$key}) { $areEqual=0; last; } } } if($areEqual) { print "ARE "; } else { print "ARE NOT "; } for (keys %hash) { $hash{$_} = $hash1{$_} if exists $hash1{$_}; } print Dumper ($hash); print Dumper($hash1); $simple->XMLout($hash, KeepRoot => 1, OutputFile => 'pets.xml', XMLDecl => "<?xml version='1.0'?>", ); $simple->XMLout($hash1, KeepRoot => 1, OutputFile => 'pets1.xml', XMLDecl => "<?xml version='1.0'?>", );
this code converts the two xml files to hash value and compare the two hash keys are same if there are same keys replace the first hash keys with second hash values what is the error and please suggest me how can i do this

Replies are listed 'Best First'.
Re: xml tag mapping?
by toolic (Bishop) on Mar 24, 2010 at 16:49 UTC
    if(keys %hash == keys %hash1) {
    This check is pointless since you have not initialized either hash. Prove it to yourself by printing their contents
    print Dumper(\%hash); print Dumper(\%hash1); if(keys %hash == keys %hash1) {
    The check will always pass:
    use strict; use warnings; my %hash; my %hash1; if (keys %hash == keys %hash1) { print "eq\n"; } else { print "neq\n"; } __END__ eq
    I have no idea if this is related to your problem since I do not understand your question. Using punctuation and capitalization in your English sentences would help to clarify.
Re: xml tag mapping?
by ikegami (Patriarch) on Mar 24, 2010 at 17:27 UTC

    I'm guessing the following happened:

    • Your code compared %hash with %hash1 even though you had no such variables.
    • strict told you as much.
    • You added my %hash; my %hash1; to silence strict rather than fixing the problem.

    You want to compare the hashes references by $hash and $hash1, not the hashes %hash and %hash1.

Re: xml tag mapping?
by grantm (Parson) on Mar 25, 2010 at 20:13 UTC

    This part of your XML is likely to cause you trouble:

    <chapter>sathish <subchap1>sathish is good boy or not <p>sathish is a good bou</p>

    The <chapter> element contains what is called 'mixed content' - a mixture of text and other elements. There is nothing inherently wrong with mixed content (XHTML for example uses it extensively) as long as you choose appropriate tools to work with it. XML::Simple is not an appropriate module for working with mixed content - it says as much in the documentation.

    You might consider changing your XML to something like:

    <chapter><title>sathish</title> <subchap1><title>sathish is good boy or not</title> <p>sathish is a good bou</p>

    Even once you have done this, I still would not recommend XML::Simple for the job. Consider reading this article on Stepping up from XML::Simple to XML::LibXML.