Hi,
Instead of using SimpleXMLElement::addAttribute
, maybe you can just use array-access to the parameters ?
For instance, considering you have that portion of code :
$str = <<<XML
<root>
<elem>glop</elem>
</root>
XML;
$xml = simplexml_load_string($str);
var_dump($xml->elem);
Which gives this output :
object(SimpleXMLElement)[2]
string 'glop' (length=4)
You can add the "class=a" attribute this way :
$xml->elem['class'] .= 'a ';
var_dump($xml->elem);
Which will give you this output :
object(SimpleXMLElement)[5]
public '@attributes' =>
array
'class' => string 'a ' (length=2)
string 'glop' (length=4)
And, then, add the "class=b" attribute by concatenating that value to the existing value of class, this way :
$xml->elem['class'] .= 'b ';
var_dump($xml->elem);
And you obtain :
object(SimpleXMLElement)[7]
public '@attributes' =>
array
'class' => string 'a b ' (length=4)
string 'glop' (length=4)
Note the space at the end of the attribute's value ; maybe you'll want to clean this, using trim :
$xml->elem['class'] = trim($xml->elem['class']);
var_dump($xml->elem);
Et voila :
object(SimpleXMLElement)[8]
public '@attributes' =>
array
'class' => string 'a b' (length=3)
string 'glop' (length=4)
Hope this helps :-)