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

标签 php xml simplexml

<分区>

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

关于php - 使用 PHP asXML 和 SimpleXMLElement 获取编码 ="UTF-8"独立 ="yes",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16386289/

相关文章:

java - 以编程方式创建的 View 显示不正确 (android)

php - 在 PHP 中将 Mysql 数据库中的数字相加

php - 无法使用 App::Laravel 中的错误定义测试代码

mysql - 加载XML更新表--MySQL

android - 解析具有相同名称标签的 XML 在 Android 中给出 null

php - 使用 PHP 解析带有命名空间的 SOAP XML 响应

php - SimpleXML 对象中的 "At sign"@?

PHP XML 解析

php - MySQL - 根据值选择列

php - 使用 PHP 将 MySQL 行转换为 XML