Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Wyciąganie danych za pomocą DOM z prostego pliku
Civil
post
Post #1





Grupa: Zarejestrowani
Postów: 86
Pomógł: 1
Dołączył: 19.09.2006
Skąd: Wrocław

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


Witam, przejrzałem posty dotyczące pobierania danych z XML za pomocą DOM ale problem w tym, że nie wiem jak zacząć.

Mam prosty plik XML

  1.      <offers>
  2.      <offer>
  3. <id>1</id>
  4. <name>Pas</name>
  5.      <description>
  6. opis
  7.  
  8. </description>
  9. <url>mod=detail&id=1</url>
  10. <image>zolw_1.jpg</image>
  11. <price>189</price>
  12. <oldprice>199</oldprice>
  13. <category>s-akcesoria</category>
  14. <undercategory>ochraniacze</undercategory>
  15. <discount>n</discount>
  16. <table>n</table>
  17. </offer>
  18. itd
  19. </offers>


I jak teraz skonstruować pętlę, która pobierze kolejne dane z offer ?

Może pytanie banalne, ale nie umiem tego ugryźć, proszę o pomoc.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Civil
post
Post #2





Grupa: Zarejestrowani
Postów: 86
Pomógł: 1
Dołączył: 19.09.2006
Skąd: Wrocław

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


Oki - jest tam taki skrypcik:

  1. <?php
  2. $indent = "";
  3. $file = "semi.xml";
  4.  
  5. $showfile = file_get_contents("c:/Program Files/Apache Group/apache/htdocs/phpxml" . "/" . $file); // whatever path
  6.  // maybe the whole path is not important, look it up in other posts
  7.  
  8. $newstring=utf8_encode($showfile); // it's important!
  9. if(!$domDocument = domxml_open_mem($newstring)) {
  10.  echo "Couldn't load xml...";  
  11.  exit;
  12. }
  13.  
  14. $rootDomNode = $domDocument->document_element();
  15. print "<pre>";
  16. printElements($rootDomNode);
  17. print "</pre>";
  18.  
  19. function printElements($domNode)
  20. {
  21. if($domNode)
  22. {
  23. global $indent;
  24.  
  25. if($domNode->node_type() == XML_ELEMENT_NODE)
  26. {
  27. print "<br />".$indent."&lt;".$domNode->node_name();
  28. if($domNode->has_attributes())
  29. {
  30. $attributes = $domNode->attributes();
  31. foreach($attributes as $domAttribute)
  32. {
  33. print " $domAttribute->name=\"$domAttribute->value\"";
  34. }
  35. }
  36. print "&gt;";
  37. if($domNode->has_child_nodes())
  38. {
  39. $indent.=" ";
  40. $nextNode = $domNode->first_child();
  41. printElements($nextNode);
  42. $indent= substr($indent, 0, strlen($indent)-2);
  43. print "<br />".$indent."&lt;"."/".$domNode->node_name()."&gt;";
  44. }
  45. else
  46. {
  47. print "$domNode->node_value()&lt;/".$domNode->node_name()."&gt;";
  48. }  
  49. }
  50.  
  51. $nextNode = $domNode->next_sibling();
  52. printElements($nextNode);
  53. }
  54. }
  55. ?>


I w jaki sposób go przerobić do mojego zastosowania?
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: 3.10.2025 - 09:34