我正在从多个方面创建一个Xml文档,我需要删除重复的 no形输出xml。 我有这样的文字,以产生新的xml文档。
#!/usr/bin/perl
use warnings;
use strict;
use XML::LibXML;
use Carp;
use File::Find;
use File::Spec::Functions qw( canonpath );
use XML::LibXML::Reader;
use Digest::MD5 md5 ;
if ( @ARGV == 0 ) {
push @ARGV, "c:/main/sav ";
warn "Using default path $ARGV[0]
Usage: $0 path ...
";
}
open( my $allxml, > , "combined.xml" )
or die "can t open output xml file for writing: $!
";
print $allxml <?xml version="1.0" encoding="UTF-8"?> ,
"
<Datainfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
";
my %extract_md5;
find(
sub {
return unless ( /(_str.xml)$/ and -f );
extract_information();
return;
},
@ARGV
);
print $allxml "</Datainfo>
";
sub extract_information {
my $path = $_;
if ( my $reader = XML::LibXML::Reader->new( location => $path )) {
while ( $reader->nextElement( Data )) {
my $elem = $reader->readOuterXml();
my $md5 = md5( $elem );
print $allxml $reader->readOuterXml() unless ( $extract_md5{$md5}++ );
}
}
return;
}
But from above script printing xml file like this
合计:xml
<?xml version="1.0" encoding="UTF-8"?>
<Datainfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<data>
<test>22</test>
<info>sensor value</info>
<sensor>
<sensor value="23" temp="25"/>
</sensor>
</data>
<data>
<test>23</test>
<info>sensor value</info>
<sensor>
<sensor value="24" temp="27"/>
</sensor>
</data>
<data>
<test>22</test>
<info>sensor value</info>
<sensor>
<sensor value="22" temp="26"/>
</sensor>
</data>
</Datainfo>
In the above xml file I have data element test(22) is repeated in two times. I need to use test as the element to search in file if same test number is found what ever may be the information inside that node I need to delete that entire node information. I tried to do with md5 but it removing duplicate nodes from allxml files but now I need to search one specific element and delete entire node information if duplicate is occurred.please help me with this problem.
output like this
合计:xml
<?xml version="1.0" encoding="UTF-8"?>
<Datainfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<data>
<test>22</test>
<info>sensor value</info>
<sensor>
<sensor value="23" temp="25"/>
</sensor>
</data>
<data>
<test>23</test>
<info>sensor value</info>
<sensor>
<sensor value="24" temp="27"/>
</sensor>
</data>
</Datainfo>