Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change attribute value on a simpleXML element?

Tags:

php

xml

simplexml

I parsed a XML file with SimepleXML:

<element data="abc,def">
 EL
</element>

But now I want to append something to the "data" attribute. Not in the file, but in my variables (in the object structure that I got from simplexml_load_file).

How can I do that?

like image 884
Naamah Avatar asked Dec 03 '22 07:12

Naamah


2 Answers

Untested, but should work:

$element->attributes()->data = ((string) $element->attributes()->data) . ',ghi';
like image 161
mjec Avatar answered Dec 05 '22 20:12

mjec


Well, it can be done like $element['data'] .= ',ghi';

like image 30
Izhar Aazmi Avatar answered Dec 05 '22 19:12

Izhar Aazmi