Getting encoding = "UTF-8" standalone = "yes" using PHP asXML and SimpleXMLElement

I use the following code to get the XML file when calling the php page:

i.e. when you enter http://example.com/strtoxmp.php , it will return you xml based on the following encoding:

  $xml = new SimpleXMLElement("<feed/>"); $feed = $xml; $feed->addChild('resultLength', "1"); $feed->addChild('endIndex', "1"); $item = $feed->addChild('item',""); $item->addChild('contentId', "10031"); $item->addChild('contentType', "Talk"); $item->addChild('synopsis', "$newTitle" ); $item->addChild('runtime', ' '); } Header('Content-type: text/xml; charset:UTF-8; '); print($xml->asXML()); 

and it works fine. It creates the following xml:

 <?xml version="1.0"?> <feed> <resultLength>1</resultLength> <endIndex>1</endIndex> <contentId>10031</contentId> <contentType>Talk</contentType> <synopsis>Mark Harris - Find Your Wings</synopsis> <runtime> </runtime> </item> </feed> 

But i need

<? xml version = "1.0" encoding = "UTF-8" standalone = "yes"? >

instead

<? xml version = "1.0"? >

+4
source share
1 answer

To specify an XML declaration, simply add it to the line that you pass to the SimpleXMLElement constructor:

 $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ' . 'standalone="yes"?><feed/>'); 

This should output the following:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <feed/> 
+20
source

Source: https://habr.com/ru/post/1479252/


All Articles