我一直在嘗試使用XML::LibXML
模塊拆分XML數據拆分XML文件,但它拋出這樣錯誤嘗試在使用XML ::的libxml模塊
Can't call method "findnodes" without a package or object reference
我輸入一個錯誤
<xml>
<bhap id="1">
<label>cylind - I</label>
<title>premier</title>
<rect id="S1">
<title>Short</title>
<label>1.</label>
<p><text>welcome</text></p>
</rect>
<rect id="S2">
<title>Definite</title>
<label>2.</label>
<p><text>welcome1</text></p>
</rect>
</bhap>
<bhap id="2">
<label>cylind – II</label>
<title>AUTHORITIES AND ITS EMPLOYEES</title>
<rect id="S3">
<title>nauty.—</title>
<label>3.</label>
<p><text>welcome3</text></p>
</rect>
<rect id=S4">
<title>Term</title>
<label>4.</label>
<p><text>welcome4</text></p>
</rect>
</bhap>
</xml>
輸出需要
文件1
<xml>
<bhap id="1">
<label>cylind - I</label>
<title>premier</title>
<rect id="S1">
<title>Short</title>
<label>1.</label>
<p><text>welcome</text></p>
</rect>
</bhap>
</xml>
文件2
<xml>
<bhap id="1">
<label>cylind - I</label>
<title>premier</title>
<rect id="S2">
<title>Definite</title>
<label>2.</label>
<p><text>welcome1</text></p>
</rect>
</bhap>
</xml>
文件3
<xml>
<bhap id="2">
<label>cylind – II</label>
<title>AUTHORITIES AND ITS EMPLOYEES</title>
<rect id="S3">
<title>nauty.—</title>
<label>3.</label>
<p><text>welcome3</text></p>
</rect>
</bhap>
</xml>
文件4
<xml>
<bhap id="2">
<label>cylind – II</label>
<title>AUTHORITIES AND ITS EMPLOYEES</title>
<rect id=S4">
<title>Term</title>
<label>4.</label>
<p><text>welcome4</text></p>
</rect>
</bhap>
</xml>
我的代碼
use XML::LibXML;
my $file = shift || die "usage $0 <xmlfile>";
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($file);
my @nodes = $doc->findnodes('//bhap');
foreach my $node1 (@nodes) {
my $bhap = $node1->toString(), "\n";
if ($bhap =~ m/(<bhap.+?>.+?<\/title>)(.+?)(<\/bhap>)/is) {
my $bhap1 = $1;
my $bhap2 = $2;
my $bhap3 = $3;
my $nodes1 = $bhap->findnodes('//rect');
foreach my $node (@$nodes1) {
my $rect = $node->toString();
if ($rect =~ m/(<rect\s*id="(.+?)">.+?<\/rect>)/is) {
my $var1 = $1;
my $var2 = $2;
print "file" $var2;
print "<xml>" print $bhap1;
print $var1;
print $bhap3;
print "</xml>";
}
}
}
}
是xml_split選項:http://search.cpan.org/dist/XML-Twig/tools/xml_split/xml_split – Sobrique 2015-03-25 09:36:39
您分配給'$ bhap'等,然後從'$ bhap'讀取。使用'使用警告;嚴格使用;'抓住這樣的事情。 – reinierpost 2015-03-25 11:21:07
'my $ nodes1 = $ bhap-> findnodes('// rect');'你在這裏對一個字符串調用'findnodes'。 – nwellnhof 2015-03-25 11:56:00