使用 XML:libXML 添加新节点抛出错误

Adding a new node using XML:libXML throwing error

这是 xml 的样子

<RasdItemsList xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:rasd="...">
      <Link rel="edit" type="application/vnd.vmware.vcloud.rasdItemsList+xml" href="..."/>
      <Item>
          <rasd:AddressOnParent>0</rasd:AddressOnParent>
          <rasd:Description>Hard disk</rasd:Description>
          <rasd:ElementName>Hard disk 1</rasd:ElementName>
          <rasd:HostResource xmlns:ns12="http://www.vmware.com/vcloud/v1.5" ns12:capacity="59392" ns12:busSubType="lsilogic" ns12:busType="6"></rasd:HostResource>
          <rasd:InstanceID>2000</rasd:InstanceID>
          <rasd:Parent>2</rasd:Parent>
          <rasd:ResourceType>17</rasd:ResourceType>
      </Item>
      <Item>
          <rasd:AddressOnParent>1</rasd:AddressOnParent>
          <rasd:Description>Hard disk</rasd:Description>
          <rasd:ElementName>Hard disk 2</rasd:ElementName>
          <rasd:HostResource xmlns:ns12="http://www.vmware.com/vcloud/v1.5" ns12:capacity="4" ns12:busSubType="lsilogic" ns12:busType="6"></rasd:HostResource>
          <rasd:InstanceID>2001</rasd:InstanceID>
          <rasd:Parent>2</rasd:Parent>
          <rasd:ResourceType>17</rasd:ResourceType>
      </Item>
.. Some more items
</RasdItemsList>

我正在尝试为此添加一个新的项目节点,即使用 XML::libXML 的新硬盘。这是我的代码

  my $doc    = $parser->parse_string ($data_xml); 
  my $xpc = XML::LibXML::XPathContext->new($doc);
  $xpc->registerNs('x', 'http://www.vmware.com/vcloud/v1.5');
  $xpc->registerNs('rasd', 'http:...');
  $xpc->registerNs('ns2','http://www.vmware.com/vcloud/v1.5');

  # add a new node
  my $new_item = $doc->createElement("Item");
  my $new_address_on_parent = $doc-> createElement("rasd:AddressOnParent");
  my $new_element_name = $doc->createElement("rasd:ElementName"); 
  my $new_description = $doc->createElement("rasd:Description");
  my $new_InstanceID  = $doc->createElement("rasd:InstanceID");
  my $new_parent = $doc->createElement("rasd:Parent");
  my $new_resource_type = $doc->createElement("rasd:ResourceType");
  my $new_host_resource = $doc->createElement("rasd:HostResource");
  if( my ($node) =  $xpc->findnodes('/x:RasdItemsList/x:Item') )
  {
    my $new_parent =  $node->parentNode;
    $new_parent->appendChild($new_item);
    $new_item->appendChild($new_address_on_parent);
    $new_item->appendChild($new_description);
    $new_item->appendChild($new_element_name);
    $new_item->appendChild($new_host_resource);
    $new_item->appendChild($new_InstanceID);
    $new_item->appendChild($new_parent);
    $new_item->appendChild($new_resource_type);
    $new_address_on_parent->appendText('4');
    $new_description->appendText('Hard disk');
    $new_element_name->appendText('Hard Disk 4');
    $new_host_resource->appendText("What to add");
    $new_InstanceID->appendText('2004');
    $new_parent->appendText('2');
    $new_resource_type->appendText('17');

这是抛出错误 "appendChild: HIERARCHY_REQUEST_ERR"。我尝试一个接一个地添加 child,它一直有效,直到我添加了 Elementname。之后,无论我尝试添加什么,都会出现上述错误。

我也不确定如何添加 rasd:HostResource child 文本。

在此先感谢您的帮助。

看看error message description

HIERARCHY_REQUEST_ERR 是

"Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to insert is one of this node's ancestors"

您正在将 $new_item($doc 的子节点)添加到您找到的节点的父节点。我猜测 $new_item 是那个节点的祖先。

您正在寻找一个 "Item" 节点,它适合:向 $doc 添加一个子节点(此处:$new_item)将比 Item 高一个级别,至少在 RasdItemsList 以下在层次结构中。

希望对您有所帮助!

$new_parent 是罪魁祸首。它已经被用作项目节点的父节点,因此不能再次使用以添加为项目的子节点。以下行正在创建问题。 $new_item->appendChild($new_parent);

我修复了这两行并且它起作用了。

 my $new_parent =  $node->parentNode;
 $new_parent->appendChild($new_item);

to 
my $new_parent_node = $node->parentNode;
$new_parent_node->appendChild($new_item);