Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [XML][PHP] Dodanie dziecka na początek
pspgor
post 1.07.2012, 16:16:49
Post #1





Grupa: Zarejestrowani
Postów: 81
Pomógł: 0
Dołączył: 27.02.2011

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


Witam. Mam owy plik XML :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <photos>
  3.  
  4. <config>
  5. ...
  6. </config>
  7.  
  8. <photo>
  9. <thumbnail>s_photo_101.jpg</thumbnail>
  10. <filename>photo_101.jpg</filename>
  11. <description><![CDATA[<p class="subtitle">Photo Title 01 goes here</p><p><a href="http://www.flashmo.com/donate" target="_parent">Lorem ipsum dolor sit amet</a>, consectetur adipiscing elit. <span class="highlight">Quisque at ante sit amet</span> erat laoreet <u>fermentum</u>. Quisque nec nisl. Nam scelerisque cursus dolor. Duis nulla diam, posuere ac, varius id, ullamcorper sit amet, libero.</p><p><span class="note">This text is dynamically loaded from the external XML file. This textfield supports HTML and CSS.</span></p>]]></description>
  12. </photo>
  13.  
  14. <photo>
  15. <thumbnail>s_photo_102.jpg</thumbnail>
  16. <filename>photo_102.jpg</filename>
  17. <description><![CDATA[<p class="subtitle">Photo Title 02 goes here</p><p><a href="http://www.flashmo.com/page/1" target="_blank">Fusce quis diam sit</a> amet tortor luctus pellentesque. Donec accumsan urna non elit tristique mattis. <b>Vivamus fermentum</b> orci viverra nisl. Quisque nec nisl.</p>]]></description>
  18. </photo>
  19.  
  20. ...


Do tego mam php:

  1. //wczytujemy plik z xmlem
  2. $xml = simplexml_load_file($plikxml);
  3.  
  4. //wyszukujemy interesująca nas gałąź
  5. $xmlCopy = $xml[0];
  6.  
  7. photo = $xmlCopy->addChild("photo","");
  8. $dziecko = photo->addChild("thumbnail", "s_".$nazwa);
  9. $dziecko = photo->addChild("filename", $nazwa);
  10. $dziecko = photo->addChild("description", $desc);
  11.  
  12. //zapisujemy XML'a do nowego pliku
  13. $xml->asXML($plikxml);


Jednakże po tym dziecko zostaje dodane na koniec, a koniecznie chciałbym, aby dodane zostało na początek.

W jaki sposób to zrobić?

Pozdrawiam.
Go to the top of the page
+Quote Post
Crozin
post 1.07.2012, 16:20:02
Post #2





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


Chyba konieczne będzie skorzystanie z innego narzędzia do obróbki XML-a, np. DOMDocument.
Go to the top of the page
+Quote Post
pspgor
post 1.07.2012, 16:28:19
Post #3





Grupa: Zarejestrowani
Postów: 81
Pomógł: 0
Dołączył: 27.02.2011

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


A jest jakaś szybka metoda na zrobienie tego co ja, tyle, że w DOMDocument?

// Ok, poradziłem sobie.

Dla zainteresowanych:

http://stackoverflow.com/questions/2092012...child-in-a-node

Ten post edytował pspgor 1.07.2012, 17:21:04
Go to the top of the page
+Quote Post
tolomei
post 1.07.2012, 18:09:13
Post #4





Grupa: Zarejestrowani
Postów: 450
Pomógł: 135
Dołączył: 18.11.2010
Skąd: Wschowa

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


Które rozwiązanie wykorzystałeś ?
Ja próbowałem rozwiązania użytkownika pib, ale nie wyszło.

Pozdrawiam.


--------------------
“ Computers are good at following instructions, but not at reading your mind. ”
- Donald Knuth
Go to the top of the page
+Quote Post
pspgor
post 2.07.2012, 12:15:17
Post #5





Grupa: Zarejestrowani
Postów: 81
Pomógł: 0
Dołączył: 27.02.2011

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


Wiesz co ja dodałem klasę, a w niej funkcję. Jest to pokazane dokładnie tutaj:

http://stackoverflow.com/a/2093059
Go to the top of the page
+Quote Post
tolomei
post 2.07.2012, 12:56:51
Post #6





Grupa: Zarejestrowani
Postów: 450
Pomógł: 135
Dołączył: 18.11.2010
Skąd: Wschowa

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


Dzięki wink.gif


--------------------
“ Computers are good at following instructions, but not at reading your mind. ”
- Donald Knuth
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: 24.07.2025 - 14:07