使用 PHP asXML 和 SimpleXMLElement 获取 encoding="UTF-8" standalone="yes"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16386289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting encoding="UTF-8" standalone="yes" using PHP asXML and SimpleXMLElement
提问by SJSSoft
I am using following code to get an XML file when a php page is called:
当调用 php 页面时,我使用以下代码获取 XML 文件:
i.e. when you enter http://example.com/strtoxmp.php
, it will return you xml, based on following coding:
即当您输入时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());
and it is working fine. It produces following xml:
它工作正常。它产生以下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"?>
< ?xml version="1.0" encoding="UTF-8" standalone="yes"?>
instead of
代替
< ?xml version="1.0"?>
<?xml 版本="1.0"?>
回答by Ben
To set the XML declaration, just add it to the string you pass to the SimpleXMLElement constructor:
要设置 XML 声明,只需将其添加到传递给 SimpleXMLElement 构造函数的字符串中:
$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/>