Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comparing two XML files with XML::Simple using FOREACH and IF using

by gr.d (Novice)
on Jan 13, 2016 at 05:59 UTC ( [id://1152628]=perlquestion: print w/replies, xml ) Need Help??

gr.d has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, So I earlier posted a similar unclear question to which I was not able to understand the answer.

I have a STANDARD.XML and I have separated them into two XML files because as a single file, they were inconsistent with the data that I'm trying to parse. Let's say they are TOP.XML and LOWER.XML. Now I'm trying to stores these files in an array and compare them.How can one store the entire file in an array?

By separating the XML files I have tried to fetch the attributes values separately from each of the XML files and write them to a single output file, Now I have to compare the two XML files and declare, (i.e.) Hint:"foreach of the LOWER.XML data, if the data "NOT" present in TOP.XML, then it can be declared as the output or written into that single file"

Does it mean I have to again create another file and write them into that and move it back to original file, if that's how it's written, how to move them to a original file?

My snippet would look something like the below

foreach (<TMP1>) { my $tmp_1 = $_; chomp ($tmp_1); print LOG $gen; foreach (<TMP2>) { my $tmp_2 = $_; chomp($tmp_2); if($tmp_1 ne $tmp_2) { print "wire"; } } }

I'm using XML::Simple part, though he's not the right one, currently I have gone almost the whole way with it, just a little bit more!

Thanks!

  • Comment on Comparing two XML files with XML::Simple using FOREACH and IF using
  • Download Code

Replies are listed 'Best First'.
Re: Comparing two XML files with XML::Simple using FOREACH and IF using
by Corion (Patriarch) on Jan 13, 2016 at 08:02 UTC

    I recommend converting the XML to "text" by reformatting it as one tag/closing tag per line and then using diff or Algorithm::Diff to find the differences.

    The nice approach is that you can find "good" diffs for structural changes, which a tree based diff does usually not model in a good way IMO.

Re: Comparing two XML files with XML::Simple using FOREACH and IF using
by mr_ron (Chaplain) on Jan 14, 2016 at 16:33 UTC

    > Now I'm trying to stores these files in an array and compare them.
    > How can one store the entire file in an array?
    XML is a tree structure and a single array isn't really designed to be the most intuitive or simple way to store a tree.

    > Now I have to compare the two XML files
    Comparing XML files is a problem many people need to solve these days so a CPAN search might help. After looking through the modules in the CPAN search results and some experimenting I came up with XML::SemanticDiff. The sample code and output might give further ideas on how the module might be applied to the posted problem.

    use strict; use warnings; use XML::SemanticDiff; my $x = XML::SemanticDiff->new; my @diffs = $x->compare( <<EOXML_1, <top> <firstTag>abc</firstTag> <secondTag>def</secondTag> <thirdTag>ghi</thirdTag> </top> EOXML_1 <<EOXML_2 <top> <firstTag>abc</firstTag> <secondTag>456</secondTag> <lastTag>ghi</lastTag> </top> EOXML_2 ); foreach my $change (@diffs) { print "$change->{message}\n\tin XPath context $change->{context} +\n"; } __END__ Character differences in element 'secondTag'. in XPath context /top[1]/secondTag[1] Child element 'thirdTag' missing from element '/top[1]'. in XPath context /top[1] Rogue element 'lastTag' in element '/top[1]'. in XPath context /top[1]
    Ron
Re: Comparing two XML files with XML::Simple using FOREACH and IF using
by anonymized user 468275 (Curate) on Jan 14, 2016 at 14:26 UTC
    To compare XML sources requires a fair bit more than comparing the lines. The first approach that comes to mind is to load each file into a tree, using one of the treebuilding CPAN modules such as XML::Twig, then traversing the two trees to build e.g. a single hash of array where each hash value is an array of what is compared for a given tag. Then a second pass through that merged structure to do the actual comparison. (I can't be more specific about the comparison without knowing what is being compared).

    One world, one people

Re: Comparing two XML files with XML::Simple using FOREACH and IF using
by Jenda (Abbot) on Jan 21, 2016 at 15:54 UTC

    There's no point in writing them into a text file. You should parse the TOP.xml into a convenient datastructure (XML::Rules makes that easy) and then as you parse the LOWER.xml you can look the data up in that structure and compare what needs to be compared.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1152628]
Approved by Corion
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-19 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found