[英]PHP How to read XML encoding from DOMDocument
我正在使用PHPs DOMDocument以可讀格式輸出XML文檔。 我想通過在響應頭中設置字符編碼來告訴瀏覽器文檔的編碼方式。 XML聲明如下: <?xml version="1.0" encoding="ISO-8859-1"?>
這是我的代碼:
$dom = new \DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$loaded = $dom->loadXML($xml_string);
$encoding = $dom->getAttribute('encoding'); //This doesn't work
echo $dom->saveXML();
我找不到使用$dom
(DOMDocument)中的方法從XML中獲取編碼“ ISO-8859-1”的方法。 我知道我可以使用正則表達式來獲取它,但是不存在從DOMDocument對象獲取encoding屬性之類的東西嗎? $dom->getAttribute('encoding')
無效。
我當時正在尋找復雜的東西,但實際上卻很簡單: $encoding = $dom->encoding
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.