This is wrong:
$current = ord($value{$i});
if (($current == 0x9) ||
($current == 0xA) ||
($current == 0xD) ||
(($current >= 0x20) && ($current <= 0xD7FF)) ||
(($current >= 0xE000) && ($current <= 0xFFFD)) ||
(($current >= 0x10000) && ($current <= 0x10FFFF)))
{
if($current != 0x1F)
$ret .= chr($current);
}
ord()
never returns anything bigger than 0xFF since it works in a byte-by-byte manner.
I'm guessing your XML is invalid because the file contains an invalid UTF-8 sequence (indeed , i.e., 0xFFFF, is invalid in UTF-8). This probably comes from copy-paste of different XML files that have different encodings.
I suggest you use the DOM extension instead to do your XML mash-up, which handles different encodings automatically by converting them internally to UTF-8.