我發現 DOMText,如果只給建構子一個整數值,將無法返回有效的資料。我必須先將輸入強制轉換為字串
$intValue = 3252; // 郵遞區號
$some_element->appendChild( new DOMText( (string) $intValue) );
當不使用「(string)」進行強制轉換時,$root->saveXML() 的 XML 輸出將無法驗證。
data
文字節點的值。如果未提供,則建立一個空的文字節點。
範例 #1 建立新的 DOMText
<?php
$dom = new DOMDocument('1.0', 'iso-8859-1');
$element = $dom->appendChild(new DOMElement('root'));
$text = $element->appendChild(new DOMText('root value'));
echo $dom->saveXML(); /* <?xml version="1.0" encoding="iso-8859-1"?><root>root value</root> */
?>