Search code examples
phpxmlsimplexml

Construct SimpleXMLelement with xml header only in PHP


I have following code

$xml_start = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><xml></xml>');

and I need to get rid of the tags. Any tag name works, but when I remove the tags I get an exception 'String could not be parsed as XML'. I can't figure out how to initialize the SimpleXMLelement with the header string <?xml version="1.0" encoding="utf-8"?> only. Also, is it possible to somehow initialize the SimpleXMLelement object without any input data and then append to it?


Solution

  • From the Docs

    The SimpleXMLElement() constructor requires a well-formed xml document or a URL that points to an XML document. By only including a header, you are not defining the content of the document, and therefore it is by definition, not well defined.