【发布时间】:2013-03-12 00:42:23
【问题描述】:
我在将父级添加到 xml 文档时遇到问题。我得到了 xml:
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
</book>
我想为这本书添加父标签,所以它是:
<library>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
</book>
</library>
我正在使用XML::LIBXML,我已经尝试获取root权限
my $root = $doc->getDocumentElement;
并创建新元素
my $new_element= $doc->createElement("library");
然后
$root->insertBefore($new_element,undef);
最后:
my $root = $doc->getDocumentElement;
my $new_element= $doc->createElement("library");
$parent = $root->parentNode;
$root->insertBefore($new_element,$parent);
但它不会工作。还试图找到返回头节点的根的父节点,然后是addchild,但它也不起作用。
【问题讨论】:
-
更好地展示你真实的完整脚本
-
我的 $root = $doc->getDocumentElement;我的 $new_element= $doc->createElement("library"); $parent = $root->parentNode; $root->insertBefore($new_element,$parent);