Perl XML::DOM 在文件之间复制节点树

发布于 2024-11-19 02:34:05 字数 1192 浏览 4 评论 0原文

我只是尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌吟 2024-11-26 02:34:05

终于找到了。一直在规范的底部:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文