libxml2 包含更有用的方法 readString(),它會讀取並回傳元素的整個文字內容。您可以在收到開始標籤 (XMLReader::ELEMENT) 後呼叫它。您可以使用此 PHP 程式碼來模擬此方法,直到 PHP 直接呼叫底層的 libxml2 實作。
<?php
class XMLReader2 extends XMLReader
{
function readString()
{
$depth = 1;
$text = "";
while ($this->read() && $depth != 0)
{
if (in_array($this->nodeType, array(XMLReader::TEXT, XMLReader::CDATA, XMLReader::WHITESPACE, XMLReader::SIGNIFICANT_WHITESPACE)))
$text .= $this->value;
if ($this->nodeType == XMLReader::ELEMENT) $depth++;
if ($this->nodeType == XMLReader::END_ELEMENT) $depth--;
}
return $text;
}
}
?>
只需使用 XMLReader2 而非 XMLReader。