开发者

Perl XML::DOM Copy Node Tree Between Files

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 "R开发者_运维问答emoving 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";


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

setOwnerDocument (doc)

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜