# Writing DOMDocument to XML file

**URL:** https://forum.kirupa.com/t/writing-domdocument-to-xml-file/308620
**Category:** programming
**Created:** [April 27, 2010, 6:37am UTC](https://forum.kirupa.com/t/writing-domdocument-to-xml-file/308620 "2010-04-27T06:37:20Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Holmesc](https://avatars.discourse-cdn.com/v4/letter/h/9f8e36/32.png) [@Holmesc](https://forum.kirupa.com/u/Holmesc)
#### Post date: [April 27, 2010, 6:37am UTC](https://forum.kirupa.com/t/writing-domdocument-to-xml-file/308620/1 "2010-04-27T06:37:20Z")

</div>

In my lovely PHP application, I’ve loaded in an XML document as a DOM object, then edited it. However, I’m having trouble writing it to my XML file.  
My code goes something like this:

```php

$xmlDoc = new DOMDocument();
$xmlDoc->load("data.xml");
//various editations occur here, all the while keeping $xmlDoc as a DOMDocument
$document = $xmlDoc->save("data.xml");
$fp = fopen("data.xml", "w+");
fwrite($fp, $document) or die("Error writing to file");
fclose($fp);

```

I’m pretty sure that I shouldn’t be using fopen, fwrite, and fclose for a DOMDocument, but it was kind of a last-ditch attempt after I couldn’t make save() work.

Other items of note:  
-No errors are returned, it seems to think it is functioning correctly.  
-Both the .php and .xml files have all permissions enabled.  
-Yes, everything is in the same directory right now.

I’m a bit of a PHP novice, so thanks in advance for the help!
