Hello all. I have an XML document and want to insert a new node at a specific spot using SimpleXML.
The original XML is this:
<epp
xmlns="urn:ietf:params:xml:ns:epp-1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd"
>
<command>
<create>
<domain:create
xmlns:domain="urn:ietf:params:xml:ns:domain-1.0"
xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0 domain-1.0.xsd"
>
<domain:period unit="y"></domain:period>
</domain:create>
</create>
</command>
</epp>
after <domain:create>
I need to add the following node:
<domain:ns>
<domain:hostAttr>
<domain:hostName></domain:hostName>
<domain:hostAddr ip="v4"></domain:hostAddr>
</domain:hostAttr>
</domain:ns>
How can I do that? I have tried this:
$xmlObj = simplexml_load_file('myXMLFile.xml');
$nsNode = $xmlObj->command->create->children(self::OBJ_URI_DOMAIN)->create->addChild('domain:ns');
$hostAttr = $nsNode->addChild('domain:hostAttr');
$hostName = $hostAttr->addChild('domain:hostName');
$hostAddr = $hostAttr->addChild('domain:hostAddr');
$hostAddr->addAtribute('ip', 'v4');
On this first line, I'm getting this warning:
Warning: SimpleXMLElement::addChild() [simplexmlelement.addchild]: Cannot add child. Parent is not a permanent member of the XML tree
On the second line, and because of this, I'm getting:
Fatal error: Call to a member function addChild() on a non-object
Thanks in advance.
Additional notes: - The php version is higher then 5.1; - I have successfully added child nodes later on this same XML.