0

Is there a way to read a XML file even the file is broken?

I'm trying to get an XML file while the file is being written into it and output it, but I'm getting an broken error since the root element is not closed because its in the middle of being written.

Is there anyway how to achieve this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
believe me
  • 910
  • 1
  • 5
  • 24

2 Answers2

1
$broken ='<root>
     <item>foo</item>';

$d  = new DOMDocument();
$d->loadXML($broken);
echo $d->saveXML($d); // useless, only prologue
$d->recover = true; // <--  it's limited, but will try its best.
$d->loadXML($broken);
echo $d->saveXML($d);
/*
 * <?xml version="1.0" encoding="UTF-8"?>
 * <root>
 * <item>foo</item></root>
 */
Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • I have to save first the fixed XML file? – believe me Mar 11 '13 at 19:47
  • If you wish... I only used `->saveXML()` as a method to show whether or not it had recovered as much of the XML as possible or not. You can do whatever you want with the `DOMDocument` after you call `->loadXML()`. – Wrikken Mar 11 '13 at 19:49
  • Thanks, is it possible to remove the error at the top of the page? – believe me Mar 11 '13 at 19:52
  • You _could_ set `libxml_use_internal_errors(true);` before you load it, **but I wouldn't recommend it**. I assume this is not a regular occurance, and as ever in production, you set `display_errors` to false, and `log_errors` on, so you'll at least be notified / have logged that your XML is actually broken. – Wrikken Mar 11 '13 at 19:57
  • I think in my case I have to do it either way, Thanks anyway its awesome. – believe me Mar 11 '13 at 20:00
0

Either use file locking or make your writes atomic so that other processes don't see half-written files. See the middle of this answer for code for both approaches.

You could also mediate your writes through something that handles the concurrency for you and gives you consistent reads and writes, i.e. a database. It doesn't have to be a full-blown standalone process--you can use sqlite or a simple key-value store.

Community
  • 1
  • 1
Francis Avila
  • 31,233
  • 6
  • 58
  • 96