[英]Include XML encoding in PHP
我是 PHP 和 XML 的初學者。
當我編寫一個新的 xml 文件時,有人可以告訴我如何包含 XML 編碼。
以下是代碼:
writexml.php
<?php
$ids = array("1", "2", "3", "4", "5");
$names = array("Jean Claude Van Damme", "Scott Adkins", "Dolph Ludgren", "Michael Jai White", "Michael Worth");
$domdoc = new DOMDocument();
$domdoc->formatOutput = true;
$el_actionstars = $domdoc->createElement( "actionstars" );
$domdoc->appendChild( $el_actionstars );
$arr_size = count($ids);
for ($i=0; $i < $arr_size; $i++) {
$el_actionstar = $domdoc->createElement( "actionstar" );
$el_id = $domdoc->createElement( "id" );
$el_id->appendChild( $domdoc->createTextNode($ids[$i] . ""));
$el_actionstar->appendChild($el_id);
$el_name = $domdoc->createElement( "name" );
$el_name->appendChild( $domdoc->createTextNode($names[$i] . ""));
$el_actionstar->appendChild($el_name);
$el_actionstars->appendChild($el_actionstar);
}
echo $domdoc->saveXML();
$domdoc->save("actionstars.xml")
?>
The xml output is <?xml version="1.0"?>
and I want to add the encoding to make it look like <?xml version="1.0" encoding="ISO-8859-1"?>
. 請幫助...
嘗試在您的實例化中使用它:
/**
* <?xml version="1.0" encoding="UTF-8" ?>
*/
$domdoc = new DOMDocument('1.0', 'UTF-8');
見底部http://php.net/manual/en/domdocument.savexml.php
當您保存整個文檔時: DOMDocument->saveXML() 在屬性 DOMDocument->encoding 中定義的編碼中生成字符串。
當您只保存一個節點時:DOMDocument->saveXML(DOMNode) 總是在 UTF-8 中生成字符串。
您可以在構造函數中設置它
DOMDocument::__construct ([ string $version [, string $encoding ]] )
例如,
$domdoc = new DOMDocument('1.0', 'iso-8859-1');
嘗試使用new DomDocument('1.0', 'UTF-8');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.