views:

45

answers:

2

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

A: 

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
Yes you have understood my question. However but the old xml will not be valid
streetparade
If i do what you wrote i got each time i update the file with new content <?xml version="1.0"?> and more than one <?xml version="1.0"?> in a xml document is not valid xml.
streetparade
+1  A: 

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
no ididnt, is there a way to do it? if yes how
streetparade
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
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
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