1

私はPerlが初めてです。xml のルート タグとルートの終了タグを perl 変数に読み込みたい。

この通常のファイル読み取りを試しました。出来た。最初の行と最後の行を取得しています。しかし、新しい行がない場合、最初の行を信頼できない場合があります。そのため、最初の行を読み取るための正規表現を使用します。

しかし、これを行うために組み込みのPerl xml関数を探しました。私は何も見つけられませんでした。つまり、すべてが私にとって新しいものです。

これに最適なライブラリを教えてください。可能であればいくつかの例。

例えば:-

<nst:root arg='1' arg2='2' arg3='3'>
   <a>1</a>
   <b>2</b>
</nst:root>

私は2つの変数が欲しい、

$root = '<nst:root arg='1' arg2='2' arg3='3'>';
$rootClose = '</nst:root>';

このルートタグを他のxmlに置き換えたいです。助けてください。

これが私がやりたかったことです。xml ファイルがあり、実際のルート タグがあります。を使用してそのファイルを複数に分割しましたXML::Twig::xml_split。多くのファイルを取得しますが、ヘッダーが異なります。メインファイルの実際のヘッダーで子ファイルを更新したい

例えば:-

スプリットリミットは2

実際のファイルは、

<nst:root arg='1' arg2='2' arg3='3'>
       <a>1</a>
       <a>1</a>
       <a>1</a>
       <a>1</a>
       <a>1</a>
       <a>1</a>
</nst:root>

で3つのファイルに分割されますXML::Twig::xml_split。そして、プラグインは独自のヘッダーを追加します。

File1:-
<xml_split:root xmlns:xml_split="http://xmltwig.com/xml_split">
           <a>1</a>
           <a>1</a>
</xml_split:root>

File2:-
<xml_split:root xmlns:xml_split="http://xmltwig.com/xml_split">
           <a>1</a>
           <a>1</a>
</xml_split:root>

File3:-
<xml_split:root xmlns:xml_split="http://xmltwig.com/xml_split">
           <a>1</a>
           <a>1</a>
</xml_split:root>

みたいに欲しい

File1:-
<nst:root arg='1' arg2='2' arg3='3'>
           <a>1</a>
           <a>1</a>
</nst:root>

File2:-
<nst:root arg='1' arg2='2' arg3='3'>
           <a>1</a>
           <a>1</a>
</nst:root>

File3:-
<nst:root arg='1' arg2='2' arg3='3'>
           <a>1</a>
           <a>1</a>
</nst:root>
4

1 に答える 1

1

プログラムの使い方はわかりませんが、モジュールxml_splitを使用したアプローチがあります。ここでは、XML::Twig新しい要素を作成し、子の各ペアをあるツリーから別のツリーに移動します。

#!/usr/bin/env perl

use strict;
use warnings;
use XML::Twig;
use POSIX qw<ceil>;

my ($split_limit, $n) = (2, 0); 

my $twig = XML::Twig->new->parsefile( shift );
my $root = $twig->root;

for (  1 .. ceil( $root->children_count / $split_limit ) ) { 
    my $t = XML::Twig::Elt->new( $root->tag, $root->atts );
    for ( 1 .. $split_limit ) { 
        my $children = $root->first_child;
        last unless $children;
        $children->move( last_child => $t );
    }   
    $t->print_to_file( 'xmlfile-' . $n++ . '.xml' );
}

次のように実行します。

perl script.pl xmlfile

これにより、ルートの子とそのヘッダーのペアごとに 1 つのファイルが生成されます。

==> xmlfile-0.xml <==
<nst:root arg="1" arg2="2" arg3="3"><a>1</a><a>1</a></nst:root>
==> xmlfile-1.xml <==
<nst:root arg="1" arg2="2" arg3="3"><a>1</a><a>1</a></nst:root>
==> xmlfile-2.xml <==
<nst:root arg="1" arg2="2" arg3="3"><a>1</a><a>1</a></nst:root>
于 2013-11-05T10:42:40.387 に答える