9

当调用 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>

但是我需要

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

代替

< ?xml 版本="1.0"?>

4

1 回答 1

29

要设置 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/>
于 2013-05-05T16:16:22.580 回答