如何同步 2 个 xml 文件?

如何同步 2 个 xml 文件?

1k.xml我有 2 个名为和 的xml 文件1n.xml

该文件1k.xml包含一些 .txt 中不存在的附加元素和数据1n.xml。我想将其中缺少的内容复制到1n.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.xml和之间的区别1n.xml

  1. sections不存在于1n.xml
  2. verse-content不同之处在于1n.xml- 这是我想保留的一个元素
  3. 整个quote事情都不存在了——从<verse></verse>1n.xml

特点:

  1. 诸诗无有section。必须section插入到正确的位置 - 就在verse-content正确的 的上方verse-number
  2. quote元素也是随机的 - 它没有出现的顺序。

简而言之:

  1. 有没有一种软件可以同步两个文件,并带有忽略某些方面的选项?

  2. 我可以使用perl或手动执行此操作java(因为我只在这两个方面有经验)?

  3. 请向我指出一些教程或方法,通过它们我可以在除verse-content数据之外的各个方面同步这两个文件。


编辑:

基本上这些文件是圣经的两个不同版本。这些*k.xml是 King 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 并使用 ExcellentXML::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”并verse-content用 nkjv 版本替换该元素。
  • 打印输出(格式化/缩进)

相关内容