address.xml :
<?xml version="1.0" encoding="utf-8"?>
<users>
<user>
<firstname>Andi</firstname>
<surname>liu</surname>
</user>
</users>
<?php
$newElement = simplexml_load_file('address.xml');
if(!$newElement)
{
echo 'unable to import';
}else{
$newuser = $newElement->users->addChild('user');
$newuser->addChild('firstname','Mark');
$newuser->addChild('surname','Zhu');
echo $newElement->asXML();
}
?>
I got this :
Warning: SimpleXMLElement::addChild() [simplexmlelement.addchild]: Cannot add child. Parent is not a permanent member of the XML tree in/Users/PowerBook/Sites/makdb/test/makpost.php on line 12
Fatal error: Call to a member function addChild() on a non-object in /Users/PowerBook/Sites/makdb/test/makpost.php on line 13
when I add a parents tags such as <posts></posts> outside the <users></users> , the problem solved!
Is there anybody can tell me why I can't add child to <users> node?
and BTW how to add childnode at the front of its parents node?