2

嗨,我有一个 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 文件...

感谢你的帮助 !!!

4

3 回答 3

2

这是使用 XML::Twig 执行此操作的一种相对简单的方法。保存在内存中的最大大小是一个完整的子文件,以防这很重要(可能会做得更好,ROOT在内存中最多保留 1 个)。

#!/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
于 2013-04-16T11:42:43.787 回答
2

Atlast 我找到了修复.. 下面是检查计数和标签值的代码..

#!/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
于 2013-04-23T08:46:30.637 回答
0

也许你可以用

use XML::Simple;

my $xml = XMLin($your_xml);

然后像

if ($xml->{HEADER}->[0]->{ROOT}->{TAG1} == $xml->{HEADER}->[1]->{ROOT}->{TAG1}) { ... }

我实际上不知道即将发布的 xml struc

于 2013-04-16T09:34:51.427 回答