我在将父级添加到 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
它也不起作用。