0

While adding child, this error is thrown : Cannot add child. Parent is not a permanent member of the XML tree. I cannot resolve this. This is my code :

 if($visited=='FIRST')
 {
 $xml=new SimpleXMLElement("<xml/>");
 $topology=$xml->addChild("Topology_Configuration");
 $flavor=$topology->addChild("Flavor");
 $networks=$topology->addChild("Networks");
 $vms=$topology->addChild("VMs");
 $vnfs=$topology->addChild("VNFs");
 $xml->asXML('saddening.xml');
 }
 else
 {
   $xml= simplexml_load_file('saddening.xml');
   $Topology_Configuration = new SimpleXMLElement($xml->asXML());
   $vmcount=$_POST['arguments']['vmcount'];
   $flavor=$Topology_Configuration->Flavor;
   $flavor_name=$flavor->addChild($_POST['arguments']['flavorName']);
   $Topology_Configuration->asXML('saddening.xml');
 }

When it is executed for the first time, the file is created(in if part). Otherwise else part is executed. It cannot add the child and is throwing the error in line :
$flavor_name=$flavor->addChild($_POST['arguments']['flavorNa‌​me']);. Please help!!

3
  • On what line error occurs? Try getChild instead of ->Flavor Commented Aug 31, 2016 at 7:43
  • $flavor_name=$flavor->addChild($_POST['arguments']['flavorName']); On this line Commented Aug 31, 2016 at 7:55
  • can you give me the syntax of getChild? Commented Aug 31, 2016 at 8:00

2 Answers 2

1

The XML from your first run results in an XML like this:

<?xml version="1.0"?>
<xml>
  <Topology_Configuration>
    <Flavor/>
    <Networks/>
    <VMs/><VNFs/>
  </Topology_Configuration>
</xml>

So if you strip down the problem you can reproduce it with:

$Topology_Configuration = simplexml_load_file($fileName);
$flavor=$Topology_Configuration->Flavor;
$flavor->addChild('abc');

echo $Topology_Configuration->asXml();

Results in:

Warning: SimpleXMLElement::addChild(): Cannot add child. 
Parent is not a permanent member of the XML tree in

The message is a little wrong, you just try to add the element to an element that does not exists. $Topology_Configuration contains the xml element node, not the Topology_Configuration.

Here are two possible solutions:

Change the XML structure

Create the XML with the Topology_Configuration as the root element.

$topology =new SimpleXMLElement("<Topology_Configuration/>");

Change the access to the Flavor

$xml = simplexml_load_file($fileName);
$flavor=$xml->Topology_Configuration->Flavor;
$flavor->addChild('abc');
Sign up to request clarification or add additional context in comments.

Comments

0

At the first time, you can use example to add child nodes

$new_xml = new SimpleXMLElement("<root></root>");
$new_xml->addAttribute('newAttr', 'value');
$newsIntro = $new_xml->addChild('content');
$newsIntro->addAttribute('type', 'value');
Header('Content-type: text/xml');
echo $new_xml->asXML();

and result

<?xml version="1.0"?>
<news newAttr="value">
    <content type="value"/>
</news

2 Comments

Your code is simple copy-paste example and does not solve issue OP faces
This doesn't solve the problem. I am filling in the xml file in the order in which the php file is invoked. When it is invoked for the first time, the if statement code gets executed , next time onwards else part. So, I have to save and then re-open .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.