0

Each time i do

$country = $this->dom->saveXML(); // put string in country 
$this->dom->save('country.xml');

It delets the old country.xml and creates a new country.xml how can i append the old content of country.xml to the new content and save it again as country.xml

GSto
  • 41,512
  • 37
  • 133
  • 184
streetparade
  • 32,000
  • 37
  • 101
  • 123
  • How many files do you what to have in result? – Ivan Nevostruev Nov 12 '09 at 19:25
  • 1
    You'll have to rephrase your question because as it is, it doesn't much sense, especially that part: "how can i write the old content [...] to the new content" - What does that mean? – Josh Davis Nov 12 '09 at 19:26
  • hmm.. image you have file country.xml with some countrys and you fetch now some other things and if you save this the old content will be overwritten and just what you fetched will be in the file country.xml how can i prevent that? – streetparade Nov 12 '09 at 19:32
  • @Ivan Nevostruev there is just one file – streetparade Nov 12 '09 at 19:35

2 Answers2

1

Rather than trying to append it to the file, have you considered appending the new data to the existing 'old' DOM and then saving that to country.xml? This way will keep the document consistent and valid.

Sean
  • 1,246
  • 9
  • 15
  • If you use `$this->dom = domxml_open_file('country.xml')`, it will create a new DOM from the file, and you can then append to it as you like. – Sean Nov 12 '09 at 20:10
  • hmm.. how can i put the content of the old file to a array? if i can to that i can write the new xml in a foreach loop thats easy – streetparade Nov 12 '09 at 20:17
  • I've just realised, the above function will only work in PHP 4. If you're using PHP 5, you'll want to use `$this->dom->load('country.xml')`. – Sean Nov 12 '09 at 20:26
0

If I understand correctly then you want to append the new XML to the end of the old file? This is a bit unusual for XML, however you could do it simply by writing to the file using

file_put_contents('country.xml', $country, FILE_APPEND);

rather than

$this->dom->save('country.xml');
tloach
  • 8,009
  • 1
  • 33
  • 44