As long as the XML file is not well formed, SimpleXML will not load it... So, you might need to do some string operations on that XML file before feeding to SimpleXML.
Something quite simple, based on str_replace might do, if this "MEMCACHE empty" thing is always the same ; else, some regex will probably do the trick ;-)
So :
- get the remote file into a string
- suppress the thing at the beginning
- give that string to
simplexml_load_string
Maybe it's not really "clean"... But should work, is fast, and simple...
For instance, if your non-XML looks like this :
$xml_string = <<<XML
<!-- MEMCACHE empty -->
<?xml version="1.0" ?>
<data>
<glop>TEST</glop>
<other>GLOP</other>
</data>
XML;
You might want to use this :
$real_xml_string = str_replace("<!-- MEMCACHE empty -->\n", '', $xml_string);
Note the "\n
" at the end : you need to remove that newline ;-)
Which gives you a string containing :
<?xml version="1.0" ?>
<data>
<glop>TEST</glop>
<other>GLOP</other>
</data>
Which is well-formed XML ; so you can now load it :
$xml = simplexml_load_string($real_xml_string);
var_dump($xml);
And you get what you wanted :
object(SimpleXMLElement)[1]
public 'glop' => string 'TEST' (length=4)
public 'other' => string 'GLOP' (length=4)
If the "status" in the MEMCACHE thing is not always "empty", you might use some regex ; something like this, I guess, might do, instead of the str_replace
call :
$real_xml_string = preg_replace("#<!-- MEMCACHE (\w+) -->\n#", '', $xml_string);
(Might need to be adapted a bit, depending on your needs)
Of course, in your case, $xml_string
would not be stored in the source code, but obtained via something like curl or file_get_contents
, I suppose.