Perl XML::DOM Copy Node Tree Between Files

南笙酒味 提交于 2019-12-10 15:38:09

问题


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";

回答1:


Finally found it. All the way at the bottom of the spec:

setOwnerDocument (doc)

So I clone, set the clones new owner, then append.



来源:https://stackoverflow.com/questions/6605158/perl-xmldom-copy-node-tree-between-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!