Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> duży xml
grzegorz_g
post
Post #1





Grupa: Zarejestrowani
Postów: 259
Pomógł: 0
Dołączył: 26.10.2004

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


oto bardzo niewielki fragmanet xml'a, którego musze pobrac i zapisać do bazy.

  1.  
  2. - <Betradar>
  3. - <OO>
  4. <Sport>Baseball</Sport>
  5. <Category />
  6. <Tournament>MLB</Tournament>
  7. <Date>2009-08-27T19:05:00</Date>
  8. <AC>1</AC>
  9. <OddsType>2W</OddsType>
  10. - <OddsData>
  11. <HomeTeam>New York Yankees</HomeTeam>
  12. <AwayTeam>Texas Rangers</AwayTeam>
  13. <HomeOdds>1.40</HomeOdds>
  14. <AwayOdds>2.85</AwayOdds>
  15. </OddsData>
  16. </OO>
  17. - <OO>
  18. <Sport>Baseball</Sport>
  19. <Category />
  20. <Tournament>MLB</Tournament>
  21. <Date>2009-08-27T19:05:00</Date>
  22. <AC>1</AC>
  23. <OddsType>Total</OddsType>
  24. - <OddsData>
  25. <HomeTeam>New York Yankees</HomeTeam>
  26. <AwayTeam>Texas Rangers</AwayTeam>
  27. <Totalscore>9.50</Totalscore>
  28. <OverOdds>1.80</OverOdds>
  29. <UnderOdds>1.90</UnderOdds>
  30. </OddsData>
  31. </OO>
  32. - <OO>
  33. <Sport>Cricket</Sport>
  34. <Category />
  35. <Tournament>One Day Internationals</Tournament>
  36. <Date>2009-08-28T11:15:00</Date>
  37. <AC>1</AC>
  38. <OddsType>2W</OddsType>
  39. - <OddsData>
  40. <HomeTeam>Scotland</HomeTeam>
  41. <AwayTeam>Australia</AwayTeam>
  42. <HomeOdds>10.00</HomeOdds>
  43. <AwayOdds>1.02</AwayOdds>
  44. </OddsData>
  45. </OO>
  46. </Betradar>
  47.  


i teraz by zapisać dane z kategorii sport listuje to wszystko poprzez
  1. $xml = simplexml_load_string($plik_z_xml);
  2. foreach ($xml->OO as $channel ) {
  3.  
  4. if ($channel->Sport=="Cricket") {
  5. [zapis do bazy]
  6. }
  7.  
  8.  
  9. }
  10.  
  11.  


jest jakieś szybsze (wydajniejsze) dotarcie do samej kategorii np:Cricket
czekam na sugestie
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
grush
post
Post #2





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 21.06.2007

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


a moze Xpath w simpleXML:

  1.  
  2. <dwml>
  3. <data>
  4. <location>
  5. <location-key>point1</location-key>
  6. <point latitude="37.39" longitude="-122.07"></point>
  7. </location>
  8. </data>
  9. .....
  10. </dwml>
  11.  
  12. XPATH Query to take the latitude in more general way
  13. /dwml/data/location/point/@latitude
  14.  
  15. Where as with simple XML it is just a familiar PHP statement,
  16. $simplexml->data->location->point->attributes()->latitude
  17.  
  18. Anyway still you can use the xpath inside your simplexml code. You can execute xpath queries by calling xpath function from any SimpleXMLEelment. It will return an array of SimpleXMLElement that match your query. So for the above example your XPath query would be something like this,
  19. $simplexml= new SimpleXMLElement($xml);
  20. $lats = $simplexml->xpath('/dwml/data/location/point/@latitude');
  21. echo $lats[0];
  22.  
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: 7.10.2025 - 09:23