PHP SimpleXMLElement: How to add dynamic child with ampersand escaping

714 views Asked by At

I'm using the following code to add dynamic child to an xml node

 <?php
   $recordXML = new SimpleXMLElement("<Record></Record>");
   $rowXML = $recordXML->addChild('row');
   foreach ($array as $column => $column_value) {
                $rowXML->addChild($column,$column_value );
            }

This code gives "unterminated entity reference" warning! when there is an ampresand & in any of the $column_value, I know & can be escaped If we assign the child content as below

 $rowXML->column_name = "text & content";
 // gives <row><column_name>text &amp; content </column_name></row>
 // without any warning

Now how to use this method to add dynamic child node with ampresand escaping ?

1

There are 1 answers

0
Chris On BEST ANSWER

Basically to make it work for you would would have to do something like this:

$rowXML->{$column} = $column_value;