Perl XML::DOM 在文件之间复制节点树
我只是尝试使用 XML::DOM 将人员节点从一个 XML 添加到另一个 XML,即使我正在克隆有问题的树,我仍然收到“WRONG_DOCUMENT_ERR”,因为它表示该节点来自另一个文件。当我尝试将节点附加到新文件时,它就发生了。我做对了吗?
我什至发现正确的解决方案是导入节点,但谷歌搜索“导入站点:http://search.cpan.org/~tjmather/XML-DOM-1.44/”没有给出任何结果。现在我很想知道这怎么可能。
my $yelParser = new XML::DOM::Parser;
my $yelDoc = $yelParser->parsefile ($yelFile);
my $bwParser = new XML::DOM::Parser;
my $bwDoc = $bwParser->parsefile ($bwFile);
my @personTags = $bwDoc->getElementsByTagName("person");
foreach my $personTag (@personTags){
my $nameTag = $personTag->getElementsByTagName("name")->[0]->getFirstChild;
my $name = $nameTag->getNodeValue();
print "Name: $name\n";
print "Making clone.\n";
my $clone = $personTag->cloneNode(1);
print "Removing Bio.\n";
$clone->getElementsByTagName("biography")->[0]->getFirstChild->setNodeValue('');
print "Appending to Yellow\n";
$yelDoc->getElementsByTagName("xml")->[0]->appendChild($clone);
print "Node done.\n";
}
<STDIN>;
my $outFile = "$folderOut/$filebase";
print "Printing to file... $outFile\n";
$yelDoc->printToFile($outFile);
print "Output done.\n";
I'm just trying to add the people nodes from one XML to another with XML::DOM and even though I am cloning the tree in question, I am still getting a "WRONG_DOCUMENT_ERR" because it says the node came from another file. It happens right when I try to append the node onto the new file. Am I doing it right?
I've even found that the correct solution is to import the node, but a google search of "import site:http://search.cpan.org/~tjmather/XML-DOM-1.44/" gives nothing. Now I'm seriously wondering how this is possible.
my $yelParser = new XML::DOM::Parser;
my $yelDoc = $yelParser->parsefile ($yelFile);
my $bwParser = new XML::DOM::Parser;
my $bwDoc = $bwParser->parsefile ($bwFile);
my @personTags = $bwDoc->getElementsByTagName("person");
foreach my $personTag (@personTags){
my $nameTag = $personTag->getElementsByTagName("name")->[0]->getFirstChild;
my $name = $nameTag->getNodeValue();
print "Name: $name\n";
print "Making clone.\n";
my $clone = $personTag->cloneNode(1);
print "Removing Bio.\n";
$clone->getElementsByTagName("biography")->[0]->getFirstChild->setNodeValue('');
print "Appending to Yellow\n";
$yelDoc->getElementsByTagName("xml")->[0]->appendChild($clone);
print "Node done.\n";
}
<STDIN>;
my $outFile = "$folderOut/$filebase";
print "Printing to file... $outFile\n";
$yelDoc->printToFile($outFile);
print "Output done.\n";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
终于找到了。一直在规范的底部:
setOwnerDocument(doc)
所以我克隆,设置克隆的新所有者,然后追加。
Finally found it. All the way at the bottom of the spec:
setOwnerDocument (doc)
So I clone, set the clones new owner, then append.