Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Extraer xml @attributes con php

Estas en el tema de Extraer xml @attributes con php en el foro de PHP en Foros del Web. Hola de nuevo Estoy extrayendo los datos de un xml con: Código PHP: $xml  =   simplexml_load_file ( "http://productoType=2" , 'SimpleXMLElement' ); foreach( $xml -> product  ...
  #1 (permalink)  
Antiguo 28/07/2015, 12:32
 
Fecha de Ingreso: octubre-2011
Mensajes: 104
Antigüedad: 12 años, 5 meses
Puntos: 1
Extraer xml @attributes con php

Hola de nuevo
Estoy extrayendo los datos de un xml con:
Código PHP:
$xml =  simplexml_load_file("http://productoType=2",'SimpleXMLElement');
foreach(
$xml->product as $fila){
$fila->categories->category;
$fila->description

Con $xml->xpath("/products") extraigo (p.e):
Código HTML:
[categories] => SimpleXMLElement Object ( [category] => Zapas ) 

[properties] => SimpleXMLElement Object ( 
	[property] => 
		Array ( 
		[0] => SimpleXMLElement Object ( [@attributes] => 
			Array ( 
				[name] => VAL) 
				[value] => 1111111 ) 
		[1] => SimpleXMLElement Object ( [@attributes] => 
			Array ( 
				[name] => manufacturer ) 
				[value] => Nike) 
		[2] => SimpleXMLElement Object ( [@attributes] => 
			Array ( 
				[name] => condition ) 
				[value] => new ) 
		[3] => SimpleXMLElement Object ( [@attributes] => 
			Array ( 
				[name] => deliveryCosts ) 
				[value] => 0.00 ) 
		[4] => SimpleXMLElement Object ( [@attributes] => 
			Array ( 
				[name] => stock ) 
				[value] => in stock ) 
		) )
categories lo extraigo sin problemas $fila->categories->category
El p roblema lo tengo con esta secuencia:
Código HTML:
[1] => SimpleXMLElement Object ( [@attributes] => 
			Array ( 
				[name] => manufacturer ) 
				[value] => Nike)
Con $marca= (string)$fila->properties->property[1]->attributes(); coonsigo extraer la palabra manufacturer, mi objetivo es conseguir extraer la palabra Nike.
He intentado con:
$marca= (string)$fila->properties->property[1]->attributes()->value; //creo que debería ser la correcta.
$marca= (string)$fila->properties->property[1]->attributes()->value[0];
$marca= (string)$fila->properties->property[1][0]->attributes()->value;
$marca= (string)$fila->properties->property[1][1]->attributes();
$marca= (string)$fila->properties->property[1]->attributes(value);
y etc..
A alguien le ha pasado lo mismo? A alguien se le ocurre algo? un saludo gracias....
  #2 (permalink)  
Antiguo 06/08/2015, 05:25
Avatar de cluster28  
Fecha de Ingreso: enero-2008
Ubicación: Donostia - San Sebastián
Mensajes: 756
Antigüedad: 16 años, 2 meses
Puntos: 32
Respuesta: Extraer xml @attributes con php

Si pones el xml lo intento un día de éstos en casa. Me acuerdo que algo parecido me pasó con un XML. No si opté por usar DomDocument...
  #3 (permalink)  
Antiguo 06/08/2015, 05:55
 
Fecha de Ingreso: febrero-2006
Mensajes: 155
Antigüedad: 18 años, 1 mes
Puntos: 6
Respuesta: Extraer xml @attributes con php

Hola, según lo que veo y sin mirar el XML

$marca= (string)$fila->properties->property[1]->attributes();

En marca obtienes "manufacturer", pero el resultado de ese método retorna un array, pero tu le estás haciendo un casting a string.

Por lo tanto, podrías intentar hacer lo siguiente:

$marca= $fila->properties->property[1]->attributes();
Y luego deberías tener:
echo $marca['name']; //manufacturer
echo $marca['value']; //Nike


Slds!
  #4 (permalink)  
Antiguo 31/08/2015, 17:37
 
Fecha de Ingreso: octubre-2011
Mensajes: 104
Antigüedad: 12 años, 5 meses
Puntos: 1
Respuesta: Extraer xml @attributes con php

Perdón por no contestar pero es que no había visto la respuesta, he intentado con:
Código PHP:
$marcas$fila->properties->property[1]->attributes(); 
$marca=$marcas['value']; 
y no obtengo nada, seguiré intentándolo, gracias y disculpas de nuevo

saludos!!!
__________________
pips
  #5 (permalink)  
Antiguo 01/09/2015, 11:08
 
Fecha de Ingreso: octubre-2011
Mensajes: 104
Antigüedad: 12 años, 5 meses
Puntos: 1
Respuesta: Extraer xml @attributes con php

Encontré la solución

Código PHP:
$brand$file->properties->property[1]->{'value'
saludos
__________________
pips

Etiquetas: parsear, xmlarchivo
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 00:32.