Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [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
nospor
post
Post #2





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




http://pl.php.net/manual/pl/class.domdocument.php

I przeczytaj opis wszystkich wlasciwosci a gwarantuje ci ze znajdziesz rozwiązanie smile.gif


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

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%)
-----


dzięki za odpowiedź - ale niestety nie znalazłem.
Poza
  1. <?php
  2. $doc = new DOMDocument('1.0');
  3. ?>
oraz
  1. <?php
  2. // we want a nice output
  3. $doc->formatOutput = true;
  4. ?>


nie trafiłem na nic co mogło by ustawiać format zapisywanych danych :-(
Go to the top of the page
+Quote Post
nospor
post
Post #4





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




no wlasnie chodzilo mi o formatOutput smile.gif
A tobie nie?


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
marcin__
post
Post #5





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

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


Jakby zadziałało to zapewne też by mi o to chodziło - ale niestety nie pomogło :-)
Go to the top of the page
+Quote Post
nospor
post
Post #6





Grupa: Moderatorzy
Postów: 36 557
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.


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
marcin__
post
Post #7





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

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

 



RSS Aktualny czas: 22.08.2025 - 08:31