Foros del Web » Programando para Internet » PHP »

SimpleXML no lee atributos

Estas en el tema de SimpleXML no lee atributos en el foro de PHP en Foros del Web. Hola amigos, tengo un problema al procesar archivos .xml, tras horas de buscar y buscar y probar y probar muchos códigos (todos los de php.net ...
  #1 (permalink)  
Antiguo 13/07/2009, 16:36
Avatar de anf
anf
 
Fecha de Ingreso: junio-2009
Mensajes: 30
Antigüedad: 14 años, 9 meses
Puntos: 4
SimpleXML no lee atributos

Hola amigos, tengo un problema al procesar archivos .xml, tras horas de buscar y buscar y probar y probar muchos códigos (todos los de php.net funcionan con un .xml de ejemplo pero con el mío no) me veo obligado a pediros ayuda por favor.

Tengo un archivo .xml que Flash desde AS 2.0 lo lee perfectamente pero SimpleXML no, os pego los formatos:

mi .xml:

Código:
<?xml version="1.0" encoding="utf-8"?>
<menulist>
	<menuitem img="imagen1.jpg" url="link1.html" id="1" title="Titulo1" text="Descripcion de texto1" />
	<menuitem img="imagen2.jpg" url="link2.html" id="2" title="Titulo2" text="Descripcion de texto2" />
	<menuitem img="imagen3.jpg" url="link3.html" id="3" title="Titulo3" text="Descripcion de texto3" />
</menulist>
Este funciona perfectamente en Flash AS 2.0, pero con SimpleXML no, por los atributos, para que lo lea SimpleXML tengo que tenerlo en este formato:


Código:
<?xml version="1.0" encoding="utf-8"?>
<menulist>
	<menuitem>
		<img>imagen1.jpg</img>
		<url>link1.html</url>
		<id>1</id>
		<title>Titulo1</title>
		<text>Descripcion de texto1</text>
	</menuitem>
	<menuitem>
		<img>imagen2.jpg</img>
		<url>link2.html</url>
		<id>2</id>
		<title>Titulo2</title>
		<text>Descripcion de texto2</text>
	</menuitem>
	<menuitem>
		<img>imagen3.jpg</img>
		<url>link3.html</url>
		<id>3</id>
		<title>Titulo3</title>
		<text>Descripcion de texto3</text>
	</menuitem>
</menulist>
¿alguien sabe que me falta o que estoy pasando por alto? ¿no se puede usar SimpleXML para leer atributos? seguro que es la tontería mas tonta, pego un fragmento de PHP que uso para debug (PHP5):

Código PHP:
/* Formato sin atributos perfecto, dump completo
   pero formato con atributos dump en blanco... */
$xml = new SimpleXMLElement('test.xml'nulltrue);
var_dump($xml); 
dump del que funciona:

Código:
object(SimpleXMLElement)#1 (1) { 
	["menuitem"]=> array(3) { 
		[0]=> object(SimpleXMLElement)#2 (5) { ["img"]=> string(11) "imagen1.jpg" ["url"]=> string(10) "link1.html" ["id"]=> string(1) "1" ["title"]=> string(7) "Titulo1" ["text"]=> string(21) "Descripcion de texto1" } 
		[1]=> object(SimpleXMLElement)#3 (5) { ["img"]=> string(11) "imagen2.jpg" ["url"]=> string(10) "link2.html" ["id"]=> string(1) "2" ["title"]=> string(7) "Titulo2" ["text"]=> string(21) "Descripcion de texto2" } 
		[2]=> object(SimpleXMLElement)#4 (5) { ["img"]=> string(11) "imagen3.jpg" ["url"]=> string(10) "link3.html" ["id"]=> string(1) "3" ["title"]=> string(7) "Titulo3" ["text"]=> string(21) "Descripcion de texto3" } 
	} 
}
Muchas gracias por todo.
  #2 (permalink)  
Antiguo 13/07/2009, 19:14
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 10 meses
Puntos: 2135
Respuesta: SimpleXML no lee atributos

Mmm no habría problema de leer atributos simplemente lees la llave del nodo $nodo['atributo'] y te da el atributo, como es que estas utilizando tu código?

Saludos.
  #3 (permalink)  
Antiguo 14/07/2009, 04:35
Avatar de anf
anf
 
Fecha de Ingreso: junio-2009
Mensajes: 30
Antigüedad: 14 años, 9 meses
Puntos: 4
De acuerdo Respuesta: SimpleXML no lee atributos

Tienes razón, he probado esto:

test.php

Código PHP:
<?php
$a 
"uno.xml";
$b "dos.xml";
if (
file_exists($a)) {
    
$xml_a simplexml_load_file($a);
    print 
"<p>DUMP $a START:</p><pre>";
    
var_dump($xml_a);
    print 
"</pre><p>DUMP $a END</p><p>".str_repeat("-",60)."</p>";
} else { print 
"<b>$a no existe.</b><p>".str_repeat("-",60)."</p>"; }

if (
file_exists($b)) {
    
$xml_b simplexml_load_file($b);
    print 
"<p>DUMP $b START:</p><pre>";
    
var_dump($xml_b);
    print 
"</pre><p>DUMP $b END</p><p>".str_repeat("-",60)."</p>";
} else { print 
"<b>$b no existe.</b><p>".str_repeat("-",60)."</p>"; }
?>
uno.xml

Código:
<?xml version="1.0" encoding="utf-8"?>
<menulist>
	<menuitem>
		<img>imagen1.jpg</img>
		<url>link1.html</url>
		<id>1</id>
		<title>Titulo1</title>
		<text>Descripcion de texto1</text>
	</menuitem>
	<menuitem>
		<img>imagen2.jpg</img>
		<url>link2.html</url>
		<id>2</id>
		<title>Titulo2</title>
		<text>Descripcion de texto2</text>
	</menuitem>
	<menuitem>
		<img>imagen3.jpg</img>
		<url>link3.html</url>
		<id>3</id>
		<title>Titulo3</title>
		<text>Descripcion de texto3</text>
	</menuitem>
</menulist>
dos.xml

Código:
<?xml version="1.0" encoding="utf-8"?>
<menulist>
	<menuitem 
		img="imagen1.jpg" 
		url="link1.html" 
		id="1" 
		title="Titulo1" 
		text="Descripcion de texto1" />
	<menuitem 
		img="imagen2.jpg" 
		url="link2.html" 
		id="2" 
		title="Titulo2" 
		text="Descripcion de texto2" />
	<menuitem 
		img="imagen3.jpg" 
		url="link3.html" 
		id="3" 
		title="Titulo3" 
		text="Descripcion de texto3" />
</menulist>
output:

Código:
DUMP uno.xml START:

object(SimpleXMLElement)#1 (1) {
  ["menuitem"]=>
  array(3) {
    [0]=>
    object(SimpleXMLElement)#2 (5) {
      ["img"]=>
      string(11) "imagen1.jpg"
      ["url"]=>
      string(10) "link1.html"
      ["id"]=>
      string(1) "1"
      ["title"]=>
      string(7) "Titulo1"
      ["text"]=>
      string(21) "Descripcion de texto1"
    }
    [1]=>
    object(SimpleXMLElement)#3 (5) {
      ["img"]=>
      string(11) "imagen2.jpg"
      ["url"]=>
      string(10) "link2.html"
      ["id"]=>
      string(1) "2"
      ["title"]=>
      string(7) "Titulo2"
      ["text"]=>
      string(21) "Descripcion de texto2"
    }
    [2]=>
    object(SimpleXMLElement)#4 (5) {
      ["img"]=>
      string(11) "imagen3.jpg"
      ["url"]=>
      string(10) "link3.html"
      ["id"]=>
      string(1) "3"
      ["title"]=>
      string(7) "Titulo3"
      ["text"]=>
      string(21) "Descripcion de texto3"
    }
  }
}
DUMP uno.xml END

------------------------------------------------------------

DUMP dos.xml START:

object(SimpleXMLElement)#5 (1) {
  ["menuitem"]=>
  array(3) {
    [0]=>
    object(SimpleXMLElement)#6 (1) {
      ["@attributes"]=>
      array(5) {
        ["img"]=>
        string(11) "imagen1.jpg"
        ["url"]=>
        string(10) "link1.html"
        ["id"]=>
        string(1) "1"
        ["title"]=>
        string(7) "Titulo1"
        ["text"]=>
        string(21) "Descripcion de texto1"
      }
    }
    [1]=>
    object(SimpleXMLElement)#7 (1) {
      ["@attributes"]=>
      array(5) {
        ["img"]=>
        string(11) "imagen2.jpg"
        ["url"]=>
        string(10) "link2.html"
        ["id"]=>
        string(1) "2"
        ["title"]=>
        string(7) "Titulo2"
        ["text"]=>
        string(21) "Descripcion de texto2"
      }
    }
    [2]=>
    object(SimpleXMLElement)#8 (1) {
      ["@attributes"]=>
      array(5) {
        ["img"]=>
        string(11) "imagen3.jpg"
        ["url"]=>
        string(10) "link3.html"
        ["id"]=>
        string(1) "3"
        ["title"]=>
        string(7) "Titulo3"
        ["text"]=>
        string(21) "Descripcion de texto3"
      }
    }
  }
}
DUMP dos.xml END

------------------------------------------------------------
Tiene que ser algún carácter no válido que tengo que tener en la cadena text, por que con estos funciona perfectamente, el que me sigue dando problemas es menu.xml original, voy a usar CDATA y así soluciono el problema.

Por la mañana se ven las cosas mas claras, gracias amigo, sin tu confirmación de que funciona no hubiese probado nada.

Saludos.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:27.