我有一個文件夾中的多個XML文件,所以我這樣寫的腳本結合成一個XML文件如何用perl
#!/usr/bin/perl
use warnings;
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/work";
warn "Using default path $ARGV[0]\n Usage: $0 path ...\n";
}
open(my $allxml, '>', "all_xml_contents.combined.xml")
or die "can't open output xml file for writing: $!\n";
print $allxml '<?xml version="1.0" encoding="UTF-8"?>',
"\n<Shiporder xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
my %shipto_md5;
find(
sub {
return unless (/(_stc\.xml)$/ and -f);
extract_information();
return;
},
@ARGV
);
print $allxml "</Shiporder>\n";
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 ($shipto_md5{$md5}++);
}
}
return;
}
它打印所有的XML文件到一個XML像消除XML文件中的標記名稱這個。
all_xml.combined.xml
<?xml version="1.0" encoding="UTF-8"?>
<student specification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<student>
<name>johan</name>
</student>
<student>
<name>benny</name>
</student>
<student>
<name>kent</name>
</student>
</student specification>
,但我有一個XML文件多了一個節點的信息,我試圖提取像這樣的信息,while循環。
$reader->nextElement('details');
$information = $reader->readInnerXml();
但是我如何將這些信息添加到輸出文件,請幫我解決這個問題。
這是因爲http://stackoverflow.com/q/8187077/133939 – Zaid