I have an xml sheet that is already made and I've spent hours looking online for a way to add attributes to certain elements. Any help would be great! If you need more info like parts of the XML let me know I'll be happy to provide what ever is needed!
Asked
Active
Viewed 54 times
-1
-
[What have you tried?](http://www.whathaveyoutried.com/) See [ask advice](http://stackoverflow.com/questions/ask-advice), please. – John Conde Mar 25 '13 at 00:48
-
I have tried using DOM elements as well as simpleXML. simpleXML was great and fine to use but the problem with it was that it saved the XML all on one line which I can't have as it needs to be human friendly. DOM has been very complicating for me to understand well. – Michael Libman Mar 25 '13 at 00:51
-
post a snippet of your xml – michi Mar 25 '13 at 01:05
-
please consider to acceppt an answer (click tick mark on the left) if it actually answered your question – michi Apr 14 '13 at 12:40
1 Answers
0
use simplexml for the job and DOM for outputting it nicely:
$xmlstr = <<<XML
<root>
<fruit origin="Brazil">Banana</fruit>
<fruit origin="Germany">Apple</fruit>
<fruit origin="Spain">Tomato</fruit>
</root>
XML;
$xml = simplexml_load_string($xmlstr);
$xml->fruit[0]->addAttribute("state","fresh"); // add state="fresh" to Banana
see this post on how to generate a structured output with passing $xml to DOM: PHP simpleXML how to save the file in a formatted way?