![]() |
Atributos XML y PHP Hola! Necesito obtener los atributos de un XML en PHP. El XML es algo así: <result_categories> <category id="3374" qty="13" link="http://www.1.com" /> <category id="2145" qty="7" link="http://www.2.com" /> <category id="3365" qty="2" link="http://www.3.com" /> <category id="1188" qty="1" link="http://www.4.com" /> <category id="1369" qty="1" link="http://www.5.com" /> </result_categories> Alguien me podría por favor ayudar a obtener el "id", "qty" y "link" de los resultados? Muchisimas gracias! |
Proba con esto: <?php $xml = "ruta a tu archivo xml"; function mostrar_categoria($categoria) { foreach ($categoria as $propiedad => $value) { $variable = $propiedad; $$variable = $value; } echo '<p><a href="' . $link . '"><strong>' . $id . '</strong></a> - ' . $qty . '</em>'; } $categorias = array(); $i = 0; function opening_element($parser, $elemento, $atributos) { global $categorias; global $i; if ($elemento == 'category') { foreach ($atributos as $atributo => $valor) { $categorias[$i][$atributo] = $valor; } $i++; } } function closing_element($parser, $element) { } $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false); xml_set_element_handler($parser, 'opening_element', 'closing_element'); if (!$document = @file($xml)) { exit(); } foreach ($document as $line) { xml_parse($parser, $line); } while ( list($id_categoria, $categoria) = each($categorias) ) { mostrar_categoria($categoria); } xml_parser_free($parser); ?> |
| La zona horaria es GMT -6. Ahora son las 05:56. |
Desarrollado por vBulletin® Versión 3.8.7
Derechos de Autor ©2000 - 2026, Jelsoft Enterprises Ltd.