获取编码= "UTF-8"独立= "yes"使用 PHP asXML 和 SimpleXMLElement



当php页面被调用时,我使用以下代码来获取XML文件:

。当您输入http://example.com/strtoxmp.php时,它将根据以下编码返回xml:

  $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());

,它工作正常。生成如下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>

但是我需要

& lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?>

代替

& lt;xml version = " 1.0 " ?>

要设置XML声明,只需将其添加到传递给SimpleXMLElement构造函数的字符串中:

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

应该输出如下内容:

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

最新更新