1

I have 2 xml files named 1k.xml and 1n.xml.

The file 1k.xml contains some additional elements and data which are not there in 1n.xml. I want to copy whatever is absent in 1n.xml to it, so that both the files are identical except for the text in one element.

1k.xml
<verse>
<verse-no>1</verse-no>
<section>Creation</section>
<verse-content>In the beginning God created the heaven and the earth.</verse-content>   
</verse>
...
<verse>
<verse-number>quote</verse-number>
<verse-quote>1:26, 27</verse-quote>
<quote>When Adam came from the Creator’s hand, he bore, in his physical, mental, and spiritual nature, a likeness to his Maker. “God created man in His own image” (Genesis 1:27), and it was His purpose that the longer man lived the more fully he should reveal this image—the more fully reflect the glory of the Creator. All his faculties were capable of development; their capacity and vigor were continually to increase. {Ed 15}</quote>
</verse> 

Differences between 1k.xml and 1n.xml:

  1. sections are absent in 1n.xml
  2. verse-content is different in 1n.xml - This is one element I want to retain as such
  3. The whole quote thing is absent - from <verse> to </verse> in 1n.xml

Peculiarities:

  1. All verses do not have section. The section must be inserted in the correct place - just above the verse-content of the correct verse-number.
  2. The quote element is also random - there is no order in which it appears.

In short:

  1. Is there a software which can synchronize two files with options to ignore a few aspects?

  2. Can I do it manually using perl or java (as I have experience only in these two)?

  3. Please point me to some tutorial or way through which I can synchronize these two files in every aspect except for the verse-content data.


Edit:

Basically the files are two different versions of the Bible. The *k.xml are the King James Version files and *n.xml are New King James Version.

As you know the verse numbers and file names would be identical. The only difference would be the verse text. The New King James version is a different translation of the Bible.

The KJV file has been modified by me. I manually added the sections and quotes. The NKJV files do not have them. They are raw. I want to transfer all the sections and quotes to the NKJV files without having to manually redo it.

So currently NKJV would look like:

1n.xml
<verse>
<verse-no>1</verse-no>
<verse-content>In the beginning God created heavens and the earth.</verse-content>
</verse>

There are no quotes and no section as well. The end result should be like:

End result 1n.xml:
<verse>
<verse-no>1</verse-no>
<section>Creation</section>
<verse-content>In the beginning God created heavens and the earth.</verse-content>   
</verse>
...
<verse>
<verse-number>quote</verse-number>
<verse-quote>1:26, 27</verse-quote>
<quote>When Adam came from the Creator’s hand, he bore, in his physical, mental, and spiritual nature, a likeness to his Maker. “God created man in His own image” (Genesis 1:27), and it was His purpose that the longer man lived the more fully he should reveal this image—the more fully reflect the glory of the Creator. All his faculties were capable of development; their capacity and vigor were continually to increase. {Ed 15}</quote>
</verse> 
  • I use xsltproc to process xml file, you have to lear xsl however. we can't see precisely what you expect, cat you provide 1k.xml 1n.xml and result.xml ? – Archemar Mar 8 '15 at 10:16
  • This may be a better question for stackoverflow.com, however, using a language with an XML parser may be the best option if you want to maintain any form of integrity of the resulting contents. – KM. Mar 8 '15 at 12:27
  • @Archemar I added the 1n.xml and result.xml – One Face Mar 8 '15 at 15:28
2

You can do this with perl, using the excellent XML::Twig module. Assuming I've understood you correctly, the basics task is copying the 'verse content' element from one file, and 'everything else' from another file, and making a new file.

So:

#!/usr/bin/perl
use strict;
use warnings;

use XML::Twig;

my %nkjv_content;

sub extract_content {
    my ( $twig, $verse )  = @_;
    my $verse_number      = $verse->first_child_text('verse-no');
    my $content           = $verse->first_child_text('verse-content');
    $nkjv_content{$verse} = $content;
}

my $nkjv = XML::Twig->new( twig_handlers => { 'verse' => \&extract_content } );
$nkjv ->parsefile('1n.xml');

my $merged_version = XML::Twig->new('pretty_print' => 'indented')->parsefile('1k.xml');

foreach my $verse ($merged_version) {
    my $verse_number = $verse->first_child_text('verse-no');
    print $verse_number, ":", $verse->first_child_text('verse-content'), "\n";

    #replace the content with the one from the nkjv source file. 
    $verse->first_child('verse_content')->set_content( $nkjv_content{$verse_number} );
}

$merged_version -> print();

Which will:

  • parse the nkjv file, and extract the verse-content elements into a hash.
  • load the 'n.xml' which contains most of the information you want to keep.
  • cycle through each 'verse' and replace the verse-content element with the nkjv version.
  • print the output (formatted/indented)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.