Ver Mensaje Individual
  #2 (permalink)  
Antiguo 20/05/2015, 22:20
germangutierrez
 
Fecha de Ingreso: enero-2015
Mensajes: 43
Antigüedad: 9 años, 11 meses
Puntos: 3
Respuesta: extraer info con Xquery para un XML

Aca un ejemplo usando xpath, tiene un hack feo para limpiar el salto de linea porque no me andaba el query de una



Código PHP:
Ver original
  1. <?php
  2. /*
  3. $xmlstr = "http://www.resultados-futbol.com/scripts/api/api.php?tz=Europe/Madrid&format=xml&req=get_teams&key=0bbe700a8b3bcf94832e2cd9556b8c5e&filter=espana";
  4.  
  5. $curl = curl_init();
  6. curl_setopt_array($curl, array(
  7.     CURLOPT_RETURNTRANSFER => 1,
  8.     CURLOPT_URL => $xmlstr,
  9.     CURLOPT_USERAGENT => 'Api client'
  10. ));
  11. $resp = curl_exec($curl);
  12. curl_close($curl);
  13. print_r($resp);*/
  14.  
  15. $resp = <<< EOL
  16. <?xml version="1.0" encoding="UTF-8"?>
  17. <get_teams>
  18.     <teams>
  19.         <id>
  20. 2107</id>
  21.         <nameShow>
  22. Real Madrid</nameShow>
  23.         <basealias>
  24. real-madrid</basealias>
  25.         <competition_name>
  26. Liga BBVA</competition_name>
  27.         <team_shield>
  28. http://thumb.resfu.com/img_data/escudos/medium/2107.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  29.     </teams>
  30.     <teams>
  31.         <id>
  32. 429</id>
  33.         <nameShow>
  34. Barcelona</nameShow>
  35.         <basealias>
  36. barcelona</basealias>
  37.         <competition_name>
  38. Liga BBVA</competition_name>
  39.         <team_shield>
  40. http://thumb.resfu.com/img_data/escudos/medium/429.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  41.     </teams>
  42.     <teams>
  43.         <id>
  44. 369</id>
  45.         <nameShow>
  46. Atlético</nameShow>
  47.         <basealias>
  48. atletico-madrid</basealias>
  49.         <competition_name>
  50. Liga BBVA</competition_name>
  51.         <team_shield>
  52. http://thumb.resfu.com/img_data/escudos/medium/369.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  53.     </teams>
  54.     <teams>
  55.         <id>
  56. 1102</id>
  57.         <nameShow>
  58. Sevilla</nameShow>
  59.         <basealias>
  60. sevilla</basealias>
  61.         <competition_name>
  62. Liga BBVA</competition_name>
  63.         <team_shield>
  64. http://thumb.resfu.com/img_data/escudos/medium/1102.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  65.     </teams>
  66.     <teams>
  67.         <id>
  68. 1102</id>
  69.         <nameShow>
  70. Sevilla</nameShow>
  71.         <basealias>
  72. sevilla</basealias>
  73.         <competition_name>
  74. Supercopa Euroamericana</competition_name>
  75.         <team_shield>
  76. http://thumb.resfu.com/img_data/escudos/medium/1102.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  77.     </teams>
  78.     <teams>
  79.         <id>
  80. 2647</id>
  81.         <nameShow>
  82. Valencia</nameShow>
  83.         <basealias>
  84. valencia-cf</basealias>
  85.         <competition_name>
  86. Liga BBVA</competition_name>
  87.         <team_shield>
  88. http://thumb.resfu.com/img_data/escudos/medium/2647.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  89.     </teams>
  90.     <teams>
  91.         <id>
  92. 347</id>
  93.         <nameShow>
  94. Athletic</nameShow>
  95.         <basealias>
  96. athletic-bilbao</basealias>
  97.         <competition_name>
  98. Liga BBVA</competition_name>
  99.         <team_shield>
  100. http://thumb.resfu.com/img_data/escudos/medium/347.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  101.     </teams>
  102.     <teams>
  103.         <id>
  104. 2120</id>
  105.         <nameShow>
  106. R. Sociedad</nameShow>
  107.         <basealias>
  108. real-sociedad</basealias>
  109.         <competition_name>
  110. Liga BBVA</competition_name>
  111.         <team_shield>
  112. http://thumb.resfu.com/img_data/escudos/medium/2120.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  113.     </teams>
  114.     <teams>
  115.         <id>
  116. 2716</id>
  117.         <nameShow>
  118. Villarreal</nameShow>
  119.         <basealias>
  120. villarreal</basealias>
  121.         <competition_name>
  122. Liga BBVA</competition_name>
  123.         <team_shield>
  124. http://thumb.resfu.com/img_data/escudos/medium/2716.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  125.     </teams>
  126.     <teams>
  127.         <id>
  128. 1617</id>
  129.         <nameShow>
  130. Málaga</nameShow>
  131.         <basealias>
  132. malaga</basealias>
  133.         <competition_name>
  134. Liga BBVA</competition_name>
  135.         <team_shield>
  136. http://thumb.resfu.com/img_data/escudos/medium/1617.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  137.     </teams>
  138.     <teams>
  139.         <id>
  140. 140</id>
  141.         <nameShow>
  142. Albacete</nameShow>
  143.         <basealias>
  144. albacete</basealias>
  145.         <competition_name>
  146. Segunda División</competition_name>
  147.         <team_shield>
  148. http://thumb.resfu.com/img_data/escudos/medium/140.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  149.     </teams>
  150.     <teams>
  151.         <id>
  152. 831</id>
  153.         <nameShow>
  154. Córdoba</nameShow>
  155.         <basealias>
  156. cordoba</basealias>
  157.         <competition_name>
  158. Liga BBVA</competition_name>
  159.         <team_shield>
  160. http://thumb.resfu.com/img_data/escudos/medium/831.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  161.     </teams>
  162.     <teams>
  163.         <id>
  164. 2563</id>
  165.         <nameShow>
  166. Las Palmas</nameShow>
  167.         <basealias>
  168. ud-palmas</basealias>
  169.         <competition_name>
  170. Segunda División</competition_name>
  171.         <team_shield>
  172. http://thumb.resfu.com/img_data/escudos/medium/2563.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  173.     </teams>
  174.     <teams>
  175.         <id>
  176. 2115</id>
  177.         <nameShow>
  178. Real Oviedo</nameShow>
  179.         <basealias>
  180. real-oviedo</basealias>
  181.         <competition_name>
  182. Segunda División B</competition_name>
  183.         <team_shield>
  184. http://thumb.resfu.com/img_data/escudos/medium/2115.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  185.     </teams>
  186.     <teams>
  187.         <id>
  188. 1547</id>
  189.         <nameShow>
  190. Levante</nameShow>
  191.         <basealias>
  192. levante</basealias>
  193.         <competition_name>
  194. Liga BBVA</competition_name>
  195.         <team_shield>
  196. http://thumb.resfu.com/img_data/escudos/medium/1547.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  197.     </teams>
  198.     <teams>
  199.         <id>
  200. 486</id>
  201.         <nameShow>
  202. Real Betis</nameShow>
  203.         <basealias>
  204. betis</basealias>
  205.         <competition_name>
  206. Segunda División</competition_name>
  207.         <team_shield>
  208. http://thumb.resfu.com/img_data/escudos/medium/486.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  209.     </teams>
  210.     <teams>
  211.         <id>
  212. 712</id>
  213.         <nameShow>
  214. Celta</nameShow>
  215.         <basealias>
  216. celta</basealias>
  217.         <competition_name>
  218. Liga BBVA</competition_name>
  219.         <team_shield>
  220. http://thumb.resfu.com/img_data/escudos/medium/712.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  221.     </teams>
  222.     <teams>
  223.         <id>
  224. 2113</id>
  225.         <nameShow>
  226. Real Murcia</nameShow>
  227.         <basealias>
  228. real-murcia</basealias>
  229.         <competition_name>
  230. Segunda División B</competition_name>
  231.         <team_shield>
  232. http://thumb.resfu.com/img_data/escudos/medium/2113.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  233.     </teams>
  234.     <teams>
  235.         <id>
  236. 901</id>
  237.         <nameShow>
  238. Deportivo</nameShow>
  239.         <basealias>
  240. deportivo</basealias>
  241.         <competition_name>
  242. Liga BBVA</competition_name>
  243.         <team_shield>
  244. http://thumb.resfu.com/img_data/escudos/medium/901.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  245.     </teams>
  246.     <teams>
  247.         <id>
  248. 4235</id>
  249.         <nameShow>
  250. Granada</nameShow>
  251.         <basealias>
  252. granada</basealias>
  253.         <competition_name>
  254. Liga BBVA</competition_name>
  255.         <team_shield>
  256. http://thumb.resfu.com/img_data/escudos/medium/4235.jpg?size=36x&amp;amp;ext=png&amp;amp;lossy=1&amp;amp;1</team_shield>
  257.     </teams>
  258. </get_teams>
  259. EOL;
  260. $equipos = new SimpleXMLElement(join("",explode("\n",$resp)));
  261.  
  262. foreach ($equipos->xpath("/get_teams/teams[competition_name='Liga BBVA']") as $equipo) {
  263.     echo $equipo->nameShow . ' tiene id ' . $equipo->id, PHP_EOL;
  264. }