2つのXMLファイルを同期する方法は?

2つのXMLファイルを同期する方法は?

1k.xml名前が2つ、.xmlファイルがあります1n.xml

このファイルには、1k.xml.txtにはないいくつかの追加要素とデータが含まれています1n.xml1n.xmlある要素のテキストを除いて、両方のファイルが同じになるように欠落しているコンテンツをコピーしたいと思います。

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> 

1k.xml1n.xml違いは次のとおりです。

  1. sectionsには存在しません1n.xml
  2. verse-content違いは、1n.xmlこれが私が維持したい要素の1つであるということです。
  3. すべてがもうquote存在しません。<verse></verse>1n.xml

特徴:

  1. 詩はありませんsectionsection正しい位置(verse-content右上)に挿入する必要がありますverse-number
  2. また、要素quoteはランダムです。つまり、表示される順序はありません。

簡単に言うと:

  1. 特定の側面を無視するオプションを使用して2つのファイルを同期させるソフトウェアはありますか?

  2. perlこの操作を使用するか、手動で行うことができますかjava(私はこれら2つについてのみ経験があるので)?

  3. データを除くすべての点で、これら2つのファイルを同期させるためのチュートリアルまたは方法を教えてくださいverse-content


編集する:

デフォルトでは、これらの文書は聖書の2つの異なるバージョンです。これは*k.xmlKing Jamesバージョンファイルと*n.xml新しいKing Jamesバージョンです。

ご存知のように、節番号とファイル名は同じです。唯一の違いは聖書です。ニューキングジェームズ聖書は聖書の他の翻訳です。

KJVファイルは私が修正しました。部品と見積もりを手動で追加しました。 NKJV文書にはそのような内容はありません。それらは生です。すべてのセクションと引用符を手動で再実行せずにNKJVファイルに転送したいと思います。

したがって、現在NKJVは次のようになります。

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

引用符もなく、章もありません。最終結果は次のようになります。

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> 

ベストアンサー1

PerlとExcellentモジュールを使用してXML::Twigこれを行うことができます。私があなたを正しく理解したと仮定すると、基本的な作業は、あるファイルから「セクションの内容」要素をコピーし、別のファイルから「他のすべて」をコピーして、新しいファイルを作成することです。

だから:

#!/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();

これは次のことを行います。

  • nkjvファイルを解析し、verse-content要素をハッシュとして抽出します。
  • 以下を含む "n.xml"をロードします。最大保管したい情報。
  • 各「節」を繰り返し、verse-content要素をnkjvバージョンに置き換えます。
  • 印刷出力(フォーマット/インデント)

おすすめ記事