2016-10-07 1 views
1

私は同じことをjavaやc#で簡単にやりますが、シェルスクリプトでこれをやっているのはたくさんの学習です...大きなXMLをUNIXスクリプトを使って子ノードのチャンクに基づいて分割する

私はURLのような子ノードを持つ巨大なXMLノードを持っています(100Kノードと言うことができます)。そして、各サブファイルの10Kノードでinput.xmlを分割する必要があります。タブ)。

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 

<url> 
    <loc> https://www.mywebsite.com/shopping </loc> 
    <changefreq> Weekly </changefreq> 
    <priority> 0.8 </priority> 
    <lastmod> 2016-09-22 </lastmod> 
</url> 
<url> 
    <loc> https://www.mywebsite.com/shopping </loc> 
    <changefreq> Weekly </changefreq> 
    <priority> 0.8 </priority> 
    <lastmod> 2016-09-22 </lastmod> 
</url> 
<url> 
    <loc> https://www.mywebsite.com/shopping </loc> 
    <changefreq> Weekly </changefreq> 
    <priority> 0.8 </priority> 
    <lastmod> 2016-09-22 </lastmod> 
</url> 
<url> 
    <loc> https://www.mywebsite.com/shopping </loc> 
    <changefreq> Weekly </changefreq> 
    <priority> 0.8 </priority> 
    <lastmod> 2016-09-22 </lastmod> 
</url> 
<url> 
    <loc> https://www.mywebsite.com/shopping </loc> 
    <changefreq> Weekly </changefreq> 
    <priority> 0.8 </priority> 
    <lastmod> 2016-09-22 </lastmod> 
</url> 
<url> 
    <loc> https://www.mywebsite.com/shopping </loc> 
    <changefreq> Weekly </changefreq> 
    <priority> 0.8 </priority> 
    <lastmod> 2016-09-22 </lastmod> 
</url> 
</urlset> 
+0

あなたはXMLパーサ、または単にいくつかの分割の魔法でこれをやりたいですか? – simbabque

+1

シェルからjava/C++を呼び出すことができます。 – toolic

+0

理想的には、Linux環境のデータステージジョブの後にこれを実行する必要があります。そうでなければ文字列と正規表現ではXMLパーサがうまくいくはずですが、エラーが発生しやすくなります。 – Ducati007

答えて

2

短い答えはイエス、これは完全になんとかです。

XML::Twigは、「切り取り」および「ペースト」操作とインクリメンタル解析(メモリフットプリントの削減)をサポートしています。

だからあなたのような何かをしたい:

#!/usr/bin/env perl 

use strict; 
use warnings; 

use XML::Twig; 

#new document. Manually set xmlns - could copy this from 'original' 
#instead though. 
my $new_doc = XML::Twig->new; 
$new_doc->set_root(
    XML::Twig::Elt->new(
     'urlset', { xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9" } 
    ) 
); 
$new_doc->set_pretty_print('indented_a'); 

my $elt_count = 0; 
my $elts_per_doc = 2; 
my $count_of_xml = 0; 

#handle each 'url' element. 
sub handle_url { 
    my ($twig, $elt) = @_; 
    #more than the count, we output this doc, close it, 
    #then create a new one. 
    if ($elt_count >= $elts_per_doc) { 
     $elt_count = 0; 
     open(my $output, '>', "new_xml_" . $count_of_xml++ . ".xml") 
     or warn $!; 
     print {$output} $new_doc->sprint; 
     close($output); 
     $new_doc = XML::Twig->new(); 
     $new_doc->set_root(
     XML::Twig::Elt->new(
      'urlset', 
      { xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9" } 
     ) 
    ); 
     $new_doc->set_pretty_print('indented_a'); 
    } 

    #cut this element, paste it into new doc. 
    #note - this doesn't alter the original on disk - only the 'in memory' 
    #copy. 
    $elt->cut; 
    $elt->paste($new_doc->root); 
    $elt_count++; 
    #purge clears any _closed_ tags from memory, so it preserves 
    #structure. 
    $twig->purge; 
} 

#set a handler, start the parse. 

my $twig = XML::Twig->new(twig_handlers => { 'url' => \&handle_url }) ->parsefile ('your_file.xml'); 
+0

ありがとうSobriqueすべてのurのヘルプの.... – Ducati007

関連する問題