Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [SimpleXML] Dodawanie nowych elementów
jakal
post 25.10.2007, 07:30:51
Post #1





Grupa: Zarejestrowani
Postów: 20
Pomógł: 0
Dołączył: 11.01.2004
Skąd: Lublin

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


Mam problem z dodaniem nowego elementu do xml`a.

Wygląda on tak:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <categories version="1" xmlns="http://xspf.org/ns/0/">
  3.  
  4.      <category>
  5.            <name>Nazwa kategorii</name>
  6.            <file>nazwa_pliku.php</file>
  7.      </category>
  8.  
  9. </categories>


Kategorie dodaje taką oto funkcją:
  1. <?php
  2. function addcat()
  3. {
  4. $xml = simplexml_load_file('system/application/xml/categories.xml');
  5.  
  6. $category = $xml->categories->addChild('category');
  7. $category->addChild('name', 'Nazwa nowej kategorii');
  8. $category->addChild('file', 'nowa_kategoria.php');
  9.  
  10. $data = $xml->asXML();
  11.  
  12. if (!write_file('system/application/xml/categories.xml', $data))
  13. {
  14. return FALSE;
  15. }
  16. else
  17. {
  18. return TRUE;
  19. }
  20. }
  21. ?>


Wyskakują mi takie błądy:
Cytat
A PHP Error was encountered

Severity: Warning

Message: SimpleXMLElement::addChild() [function.SimpleXMLElement-addChild]: Node no longer exists

Filename: libraries/Adminlib.php

Line Number: 24


Cytat
A PHP Error was encountered

Severity: Warning

Message: SimpleXMLElement::addChild() [function.SimpleXMLElement-addChild]: Node no longer exists

Filename: libraries/Adminlib.php

Line Number: 25


Linie 24 i 25 to:
  1. <?php
  2. $category->addChild('name', 'Nazwa kategorii');
  3. $category->addChild('file', 'nazwa pliku');
  4. ?>



W czym jest problem?


--------------------
8cells.com - tworzenie stron www
Go to the top of the page
+Quote Post
sf
post 25.10.2007, 11:05:19
Post #2





Grupa: Zarejestrowani
Postów: 1 597
Pomógł: 30
Dołączył: 19.02.2003
Skąd: Tychy

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


$xml->categories->addChild(), a nie: $xml->addChild('category'); ? (pisane na sucho, ale ta Twoja konstrukcja jest raczej zła)


--------------------
Zapraszam na mój php blog, tworzenie stron.
Go to the top of the page
+Quote Post
jakal
post 25.10.2007, 21:38:48
Post #3





Grupa: Zarejestrowani
Postów: 20
Pomógł: 0
Dołączył: 11.01.2004
Skąd: Lublin

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


Cytat(sf @ 25.10.2007, 12:05:19 ) *
$xml->categories->addChild(), a nie: $xml->addChild('category'); ? (pisane na sucho, ale ta Twoja konstrukcja jest raczej zła)


Wtedy jest jeszcze gorzej:

Cytat
A PHP Error was encountered

Severity: Warning

Message: SimpleXMLElement::addChild() expects at least 1 parameter, 0 given

Filename: libraries/Adminlib.php

Line Number: 23


pozatym ja mam:
  1. <?php
  2. $category = $xml->categories->addChild('category');
  3. ?>

a nie:
  1. <?php
  2. $xml->addChild('category');
  3. ?>


Ten post edytował jakal 25.10.2007, 21:41:56


--------------------
8cells.com - tworzenie stron www
Go to the top of the page
+Quote Post
c3zi
post 25.10.2007, 22:43:27
Post #4





Grupa: Zarejestrowani
Postów: 266
Pomógł: 20
Dołączył: 15.11.2006
Skąd: Koszalin

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


Witam, zapisz to w ten sposób:

  1. <?php
  2. ...
  3. ...
  4.  
  5. $xml = simplexml_load_file('plik.xml');
  6.  
  7. $category = $xml->addChild('category');
  8. $category->addChild('name', 'Nazwa nowej kategorii');
  9. $category->addChild('file', 'nowa_kategoria.php');
  10.  
  11. $data = $xml->asXML();
  12. ...
  13. ...
  14. ?>


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

Go to the top of the page
+Quote Post
jakal
post 26.10.2007, 11:07:14
Post #5





Grupa: Zarejestrowani
Postów: 20
Pomógł: 0
Dołączył: 11.01.2004
Skąd: Lublin

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


Dzięki, teraz wszystko działa biggrin.gif


--------------------
8cells.com - tworzenie stron www
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 Wersja Lo-Fi Aktualny czas: 19.07.2025 - 09:55