Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP][XML]XML - DOM, Zapisac pliku XML w PHP
marcin__
post
Post #1





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 17.03.2009

Ostrzeżenie: (0%)
-----


Witam
Walczę już chwile i poległem.
Czy ktoś wiem jak zapisać dane w poniższym kodzie, aby każdy nowo dodany element do pliku XML znajdował się w nowej linii.
Teraz zapisuje mi wszystko do/w jednym wierszu.

  1. <?php
  2. $xml = new DOMDocument();
  3. $xml->load('test.xml');
  4.  
  5. $xel = $xml->documentElement;
  6. $news = $xml->createElement('news');
  7.  
  8. $data = $xml->createElement('data', $_POST['data']);
  9. $tytul = $xml->createElement('tytul',$_POST['tytul']);
  10. $opis = $xml->createElement('opis',$_POST['opis']);
  11.  
  12. $news->appendChild($data);
  13. $news->appendChild($tytul);
  14. $news->appendChild($opis);
  15.  
  16. $xel->appendChild($news);
  17. $xml->save("test.xml");
  18. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




i po zastosowaniu tego wogole ci sie nic nie poprawilo?
No to pokaz kod jak ty tego uzyles.
Go to the top of the page
+Quote Post
marcin__
post
Post #3





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 17.03.2009

Ostrzeżenie: (0%)
-----


oto kod z "formatOutput = true;" i jak już wcześniej pisałem do pliku tets.xml skrypt zapisuje mi wszystko w jednej linii, a chciałbym aby był zachowany format pliku xml.

  1. <?php
  2. $xml = new DOMDocument('1.0');
  3. $xml->load('test.xml');
  4. $xml->formatOutput = true;
  5. $xel = $xml->documentElement;
  6.  
  7. $news = $xml->createElement('news');
  8. $xel->appendChild($news);
  9.  
  10. $zdjecie= $xml->createElement('zdjecie',$file);
  11. $news->appendChild($zdjecie);
  12.  
  13. $data = $xml->createElement('data', $_POST['data']);
  14. $news->appendChild($data);
  15.  
  16. $tytul = $xml->createElement('tytul',$_POST['tytul']);
  17. $news->appendChild($tytul);
  18.  
  19. $opis = $xml->createElement('opis',$_POST['opis']);
  20. $news->appendChild($opis);
  21.  
  22. $opis2 = $xml->createElement('opis2',$_POST['opis2']);
  23. $news->appendChild($opis2);
  24.  
  25. $xml->save("test.xml");
  26. ?>


a poniżej test.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <aktualnosci>
  3. <news><zdjecie>/news/foto/4_1_112.gif</zdjecie><data>2009-03-15</data><tytul>test no1</tytul><opis>Opis 1</opis><opis2>opis 2</opis2></news></aktualnosci>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 10.10.2025 - 15:21