my question is really simple and it's just out of curiosity.
I have a string formatted as XML <node><child attr="value">data</child><child attr="value">data</child></node>...
and I need to convert it to SimpleXML. I don't have any issue, I know how to do it and how to handle it, I just wanna know what's the difference between
$xml_data = new SimpleXMLElement($string);
and
$xml_data = simplexml_load_string($string);
I also would like to know if I am supposed to save the string as a well formatted XML file, with the header <?xml version=”1.0″ encoding=”utf-8″?>
to avoid any issue. By the way I only need the data to be handled as an array and I will never save it as a file so keep it in mind when you answer
Thanks in advance for the answers
It's just 2 different ways of doing it, the only difference is that the __construct new SimpleXMLElement
takes an additional argument that allows you to load file from a uri which is the same behavior as using simplexml_load_file()
instead of _string
About the header it depends on the version of XML you are using,
1.0 is optional, 1.1 is mandatory, have a look at Does a valid XML file require an xml declaration?