tags:

views:

328

answers:

2
 <!-- MEMCACHE empty -->

The above is now causing issues for a script as it is now placed at the top of the XML files I'm trying to remotely access. simpleXML doesn't like the fact the XML file is no longer well formed. I tried escaping the errors but that didn't seem the do the trick. Can anyone point me in the direction on how to solve this?

A: 

If it's at the very top of the file like

<!-- MEMCACHE empty -->
<?xml version="1.0" ?>
<root> ... </root>

Then I think the best thing to do would be to preg_replace("/^&lt;!-- MEMCACHE empty --&gt;$/", "", $xml_file) it to empty or null.

That's because <?xml version="1.0" ?> needs to be the first line of the file for it to be valid XML in any case, DOM, SAX, SimpleXML or otherwise.

null
+1  A: 

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
&lt;!-- MEMCACHE empty --&gt;
<?xml version="1.0" ?>
<data>
    <glop>TEST</glop>
    <other>GLOP</other>
</data>
XML;

You might want to use this :

$real_xml_string = str_replace("&lt;!-- MEMCACHE empty --&gt;\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("#&lt;!-- MEMCACHE (\w+) --&gt;\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.

Pascal MARTIN
Meant to respond sooner. Worked perfectly, thanks! After I made the changes they disabled memcache though. Go figure.
Eric
You're welcome :-) ;; bad luck, about the disabling of memcached after problem solved :-D
Pascal MARTIN