Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php5/xml] edycja pliku xml
dzobert
post 30.06.2006, 22:15:23
Post #1





Grupa: Zarejestrowani
Postów: 46
Pomógł: 1
Dołączył: 30.06.2006
Skąd: okolice Warszawy

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


Napisałem skrypt w php, który ma dodawać dane do pliku xml, ma on służyć do prostego dodawania newsów w kanale rss. Proszę pomóżcie mi bo ten skrypt nie chce działać, wyświetla się "Fatal error: Call to undefined method DOMDocument::dodajitem() on line 46", a ja nie umiem tego naprawić, jeśli ktoś wie jak to naprawić byłbym bardzo wdzięczny.

skrypt:

  1. <?php
  2.  function dodajitem($id,$title,$link,$updated,$description,$author) {
  3. $dom = simplexml_load_file('new.xml');
  4. $item = $dom->xpath("/rss/channel/item[last()]");
  5. foreach ($item as $it){
  6.  $lastID=$it->ID;
  7. }
  8. $id = $this->createElement("id");
  9. $id->appendChild($this->createTextNode(intval($lastID)+1));
  10.  
  11. $title = $this->createElement("title");
  12. $title->appendChild($this->createTextNode(iconv("ISO-8859-2","UTF-8",$tytul)));
  13.  
  14. $link = $this->createElement("link");
  15. $link->appendChild($this->createTextNode(iconv("ISO-8859-2","UTF-8",$link)));
  16.  
  17. $updated = $this->createElement("updated");
  18. $updated->appendChild($this->createTextNode(iconv("ISO-8859-2","UTF-8",$updated)));
  19.  
  20. $description = $this->createElement("description");
  21. $description->appendChild($this->createTextNode(iconv("ISO-8859-2","UTF-8",$description)));
  22.  
  23. $author = $this->createElement("author");
  24. $author->appendChild($this->createTextNode(iconv("ISO-8859-2","UTF-8",$author)));
  25.  
  26. $item = $this->createElement("item");
  27. $item->appendChild($this->createTextNode("n "));
  28. $item->appendChild($id);
  29. $item->appendChild($this->createTextNode("n "));
  30. $item->appendChild($title);
  31. $item->appendChild($this->createTextNode("n "));
  32. $item->appendChild($link);
  33. $item->appendChild($this->createTextNode("n "));
  34. $item->appendChild($updated);
  35. $item->appendChild($this->createTextNode("n "));
  36. $item->appendChild($description);
  37. $item->appendChild($this->createTextNode("n "));
  38. $item->appendChild($author);
  39. $item->appendChild($this->createTextNode("n "));
  40. $this->documentElement->appendChild($this->createTextNode(" "));
  41. $this->documentElement->appendChild($item);
  42. $this->documentElement->appendChild($this->createTextNode("n"));
  43. }
  44. $item = new DOMDocument('1.0', 'iso-8859-2');
  45. $item->load("new.xml");
  46. $item->dodajitem("0","coś","http://jakis.adres.url", "data", "opis", "emailautora");
  47. $item->saveXML();
  48. $item->save("new.xml");  
  49. ?>


plik new.xml:

  1. <?xml version="1.0" encoding="iso-8859-2"?>
  2. <rss version="2.0">
  3. <channel>
  4. <title></title>
  5. <link></link>
  6. <description></description>
  7. <language></language>
  8. <webMaster></webMaster>
  9.  <image>
  10.   <title></title>
  11.   <link></link>
  12.   <url></url>
  13.   <height></height>
  14.   <width></width>
  15.  </image>
  16.  <item>
  17.   <id></id>
  18.   <title></title>
  19.   <link></link>
  20.   <updated></updated>
  21.   <description></description>
  22.   <author></author>
  23.  </item>
  24. </channel>
  25. </rss>


Jeśli taki temat jest to przepraszam, lecz zależy mi na szybkiej odpowiedzi. Widze, że nikt nie umie mi pomóc, szkoda.

Ten post edytował dzobert 2.07.2006, 15:56:18
Go to the top of the page
+Quote Post
Sh4dow
post 4.07.2006, 12:14:09
Post #2





Grupa: Zarejestrowani
Postów: 569
Pomógł: 0
Dołączył: 17.08.2003
Skąd: Dąbrowa Górnicza

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


do tosc proste, to twoj blad logiczny

  1. <?php
  2. ...
  3. $item = new DOMDocument('1.0', 'iso-8859-2'); // <= tutaj stworzyles sobie obiekt
  4. $item->load("new.xml");
  5. $item->dodajitem("0","coś","http://jakis.adres.url", "data", "opis", "emailautora"); //a tutaj odwolujesz sie do tego obiektu i jego metody ktorej nie ma. Ty stworzyłe
    ś funkcje a nie metode
  6. $item->saveXML();
  7. $item->save("new.xml");  
  8. ?>

Mozesz stworzyc Klase ktora dziedziczy po DOMDocument i posiada dodatkowa metode. Tak to poprostu nie przejdzie.

  1. <?php
  2. class Moj_DOMDocument extends DOMDocument {
  3.  function dodajitem($id,$title,$link,$updated,$description,$author) {
  4. $dom = simplexml_load_file('new.xml');
  5. $item = $dom->xpath("/rss/channel/item[last()]");
  6. foreach ($item as $it){
  7.  $lastID=$it->ID;
  8. }
  9. $id = $this->createElement("id");
  10. $id->appendChild($this->createTextNode(intval($lastID)+1));
  11.  
  12. $title = $this->createElement("title");
  13. $title->appendChild($this->createTextNode(iconv("ISO-8859-2","UTF-8",$tytul)));
  14.  
  15. $link = $this->createElement("link");
  16. $link->appendChild($this->createTextNode(iconv("ISO-8859-2","UTF-8",$link)));
  17.  
  18. $updated = $this->createElement("updated");
  19. $updated->appendChild($this->createTextNode(iconv("ISO-8859-2","UTF-8",$updated)));
  20.  
  21. $description = $this->createElement("description");
  22. $description->appendChild($this->createTextNode(iconv("ISO-8859-2","UTF-8",$description)));
  23.  
  24. $author = $this->createElement("author");
  25. $author->appendChild($this->createTextNode(iconv("ISO-8859-2","UTF-8",$author)));
  26.  
  27. $item = $this->createElement("item");
  28. $item->appendChild($this->createTextNode("n "));
  29. $item->appendChild($id);
  30. $item->appendChild($this->createTextNode("n "));
  31. $item->appendChild($title);
  32. $item->appendChild($this->createTextNode("n "));
  33. $item->appendChild($link);
  34. $item->appendChild($this->createTextNode("n "));
  35. $item->appendChild($updated);
  36. $item->appendChild($this->createTextNode("n "));
  37. $item->appendChild($description);
  38. $item->appendChild($this->createTextNode("n "));
  39. $item->appendChild($author);
  40. $item->appendChild($this->createTextNode("n "));
  41. $this->documentElement->appendChild($this->createTextNode(" "));
  42. $this->documentElement->appendChild($item);
  43. $this->documentElement->appendChild($this->createTextNode("n"));
  44. }
  45. }
  46. $item = new Moj_DOMDocument('1.0', 'iso-8859-2');
  47. $item->load("new.xml");
  48. $item->dodajitem("0","coś","http://jakis.adres.url", "data", "opis", "emailautora"); 
  49. $item->saveXML();
  50. $item->save("new.xml");  
  51. ?>


Tak moze dzialac, jesli nie masz nigdzie bledu w skladni

guitar.gif

Ten post edytował Sh4dow 4.07.2006, 12:15:22


--------------------
Warsztat: Linux: PHP, MySQL, Apache, NetBeans, C++, Qt-Creator
Użytkownik, słowo którego specjaliści IT używają, gdy chcą powiedzieć idiota
Zarządzaj swoim budżetem domowym
Go to the top of the page
+Quote Post
dzobert
post 4.07.2006, 15:54:12
Post #3





Grupa: Zarejestrowani
Postów: 46
Pomógł: 1
Dołączył: 30.06.2006
Skąd: okolice Warszawy

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


dzięki, już działa
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: 12.05.2024 - 03:16