您好我有一个XML,我希望根据其中的标记值将其拆分为多个XML。
实施例: -
<HEADER>
<ROOT>
<TAG1>ABC</TAG1>
<TAG2>78011DAC8</TAG2>
<TAG3>US78011DAC83</TAG3>
</ROOT>
<ROOT>
<TAG1>ABC</TAG1>
<TAG2>78011DAD6</TAG2>
<TAG3>US78011DAD66</TAG3>
</ROOT>
<ROOT>
<TAG1>ABC</TAG1>
<TAG2>B06983611</TAG2>
<TAG3>GB0009075325</TAG3>
</ROOT>
<ROOT>
<TAG1>ABC</TAG1>
<TAG2>B06983629</TAG2>
<TAG3>GB0009081828</TAG3>
</ROOT>
<ROOT>
<TAG1>ABC</TAG1>
<TAG2>BRS038D62</TAG2>
<TAG3>FR0010050559</TAG3>
</ROOT>
<ROOT>
<TAG1>ABC</TAG1>
<TAG2>BRS49ESZ5</TAG2>
<TAG3>GB00B1Z5HQ14</TAG3>
</ROOT>
<ROOT>
<TAG1>DEF</TAG1>
<TAG2>B06983637</TAG2>
<TAG3>GB0008983024</TAG3>
</ROOT>
<ROOT>
<TAG1>DEF</TAG1>
<TAG2>BRS26Y2R4</TAG2>
<TAG3>GB00B128DH60</TAG3>
</ROOT>
<ROOT>
<TAG1>DEF</TAG1>
<TAG2>BRS1JW2X3</TAG2>
<TAG3>FR0010235176</TAG3>
</ROOT>
<ROOT>
<TAG1>DEF</TAG1>
<TAG2>BRS1JW2Y1</TAG2>
<TAG3>GB00B0CNHZ09</TAG3>
</ROOT>
<ROOT>
<TAG1>DEF</TAG1>
<TAG2>BRS3BP9P2</TAG2>
<TAG3>GB00B1L6W962</TAG3>
</ROOT>
<ROOT>
<TAG1>DEF</TAG1>
<TAG2>BRS7FFAV6</TAG2>
<TAG3>GB00B3D4VD98</TAG3>
</ROOT>
<ROOT>
<TAG1>DEF</TAG1>
<TAG2>B0A07E1X7</TAG2>
<TAG3>GB0031790826</TAG3>
</ROOT>
<ROOT>
<TAG1>DEF</TAG1>
<TAG2>BRS1Z0T57</TAG2>
<TAG3>GB00B0V3WQ75</TAG3>
</ROOT>
<ROOT>
<TAG1>XYZ</TAG1>
<TAG2>BRS9ZDYJ6</TAG2>
<TAG3>FR0010899765</TAG3>
</ROOT>
<ROOT>
<TAG1>XYZ</TAG1>
<TAG2>BRS8ANE14</TAG2>
<TAG3>DE0001030526</TAG3>
</ROOT>
<ROOT>
<TAG1>XYZ</TAG1>
<TAG2>BRS22TXL8</TAG2>
<TAG3>DE0001030500</TAG3>
</ROOT>
<ROOT>
<TAG1>XYZ</TAG1>
<TAG2>BRS5LHPB7</TAG2>
<TAG3>GB00B24FFM16</TAG3>
</ROOT>
<ROOT>
<TAG1>XYZ</TAG1>
<TAG2>B06983223</TAG2>
<TAG3>GB0008932666</TAG3>
</ROOT>
</HEADER>
在上面的示例中,我需要检查TAG1值,如果它与下一个TAG1值匹配则不应该拆分,如果不匹配则应该拆分为新的XML文件......
感谢您的帮助!!!
答案 0 :(得分:2)
使用XML :: Twig这是一种相对简单的方法。保留在内存中的最大大小是一个完整的子文件,如果这很重要(可能会做得更好,在内存中保留最多1 ROOT
。
#!/usr/bin/perl
use strict;
use warnings;
use autodie qw( open);
use XML::Twig;
my $in_file = $ARGV[0];
my $out_file= "$in_file.p";
my $i="01";
my $current_tag1='';
my $twig=XML::Twig->new(
twig_handlers => {
ROOT => sub { my( $t, $root)= @_;
$current_tag1||= $root->field( 'TAG1'); # initialize current tag if needed
if( $root->field( 'TAG1') ne $current_tag1) # found a break in the value of TAG1
{
$root->cut; # get the new root out of the way
$t->print_to_file( $out_file. $i++); # output the part
$t->purge; # remove the content of the part
$root->paste( first_child => $t->root); # put the new root back in place
$current_tag1= $root->field( 'TAG1');
}
}
},
keep_spaces => 1, # to keep line returns
);
$twig->parsefile($in_file);
$twig->print_to_file( $out_file . $i); # output the last part
答案 1 :(得分:2)
Atlast我发现了修复.. 下面是检查计数和TAG值的代码....
#!/usr/bin/perl
use strict;
use warnings;
use autodie qw( open);
use XML::Twig;
my $in_file = $ARGV[0];
my $out_file= "$in_file.p";
my $i="01";
my $current_tag1='';
my $previous_tag1 = '';
my $nb_root_in_file =0;
my $MIN_ROOT_IN_FILE = 5;
my $twig=XML::Twig->new(
twig_handlers => {
ROOT => sub { my( $t, $root)= @_;
$current_tag1||= $root->field( 'TAG1'); # initialize current tag if needed
$nb_root_in_file++;
if( $nb_root_in_file > $MIN_ROOT_IN_FILE && $root->field( 'TAG1') ne $current_tag1) # found a break in the value of TAG1
{
$root->cut; # get the new root out of the way
$t->print_to_file( $out_file. $i++); # output the part
$t->purge; # remove the content of the part
$root->paste( first_child => $t->root); # put the new root back in place
$current_tag1= $root->field( 'TAG1');
$nb_root_in_file =0;
}
$previous_tag1 = $current_tag1;
}
},
keep_spaces => 1, # to keep line returns
);
$twig->parsefile($in_file);
$twig->print_to_file( $out_file . $i); # output the last part
答案 2 :(得分:0)
也许你可以用
解析它use XML::Simple;
my $xml = XMLin($your_xml);
然后像
if ($xml->{HEADER}->[0]->{ROOT}->{TAG1} == $xml->{HEADER}->[1]->{ROOT}->{TAG1}) { ... }
我实际上不知道即将出现的xml struc