perl - Adding a new node using XML:libXML throwing error -


here xml looks like

<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> .. more items </rasditemslist> 

i trying add new item node this, i.e new hard disk using xml::libxml. here code

  $doc    = $parser->parse_string ($data_xml);    $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 new node   $new_item = $doc->createelement("item");   $new_address_on_parent = $doc-> createelement("rasd:addressonparent");   $new_element_name = $doc->createelement("rasd:elementname");    $new_description = $doc->createelement("rasd:description");   $new_instanceid  = $doc->createelement("rasd:instanceid");   $new_parent = $doc->createelement("rasd:parent");   $new_resource_type = $doc->createelement("rasd:resourcetype");   $new_host_resource = $doc->createelement("rasd:hostresource");   if( ($node) =  $xpc->findnodes('/x:rasditemslist/x:item') )   {     $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 add");     $new_instanceid->appendtext('2004');     $new_parent->appendtext('2');     $new_resource_type->appendtext('17'); 

this throwing error saying "appendchild: hierarchy_request_err". tried add child 1 one , works until add elementname. after whatever try add above error.

also not sure how add rasd:hostresource child text.

thanks in advance help.

have @ error message description:

hierarchy_request_err

"raised if node of type not allow children of type of newchild node, or if node insert 1 of node's ancestors"

you adding $new_item ( child of $doc ) parent of node have found. guessing $new_item ancestor of node.

you looking "item" node, fit: adding child $doc ( here: $new_item ) level above item, @ least below rasditemslist in hierarchy.

hope helps!


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -