文件之间的Perl XML :: DOM复制节点树 [英] Perl XML::DOM Copy Node Tree Between Files

查看:82
本文介绍了文件之间的Perl XML :: DOM复制节点树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想将人员节点从一个XML添加到另一个XML :: DOM中,即使我正在克隆问题树,我仍然会收到"WRONG_DOCUMENT_ERR",因为它表示该节点来自另一个节点文件.当我尝试将节点附加到新文件时,它就发生了.我做对了吗?

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?

我什至发现正确的解决方案是导入节点,但是谷歌搜索导入站点:http://search.cpan.org/~tjmather/XML-DOM-1.44/"没有任何结果.现在我很想知道这怎么可能.

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

推荐答案

最后找到了它.一直在规范的底部:

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

setOwnerDocument(doc)

setOwnerDocument (doc)

因此我进行克隆,将克隆设置为新所有者,然后追加.

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

这篇关于文件之间的Perl XML :: DOM复制节点树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆