I’m trying to add a child to an XML-node by loading a string as an xml-node, but for some reason it returns an empty value …
// Load xml
$path = 'path/to/file.xml';
$xml = simplexml_load_file($path);
// Select node
$fields = $xml->sections->fields;
// Create new child node
$nodestring = '<option>
<label>A label</label>
<value>A value</value>
</option>';
// Add field
$fields->addChild('child_one', simplexml_load_string($nodestring));
For some reason, child_one is added but without content, although it does put in the line-breaks.
Although when I do a var_export on simplexml_load_string($nodestring), I get:
SimpleXMLElement::__set_state(array(
'label' => 'A label',
'value' => 'A value',
))
So I’m not sure what I’m doing wrong …
EDIT:
Sample xml-file:
<config>
<sections>
<fields>
text
</fields>
</sections>
</config>
Sampe $xml -file after trying to add child node:
<config>
<sections>
<fields>
text
<child_one>
</child_one></fields>
</sections>
</config>
SimpleXML cannot manipulate nodes. You can create new nodes from values, but you cannot create a node then copy this node to another document.
Here are three solutions to that problem:
Create the nodes in the right document directly, e.g.
Use a library such as SimpleDOM, which will let you use DOM methods on SimpleXML elements.
In your example, solution 2 seems the best.