Reputation: 4886
I have an XML feed that has content like this
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="propiedades.xsd" generated="2015-04-28T15:16:46">
<propiedades>
<Descripcion>Excelente casa de dos pisos en condominio en la urbanización Santa Maria De Villa Club, en Carabayllo, con un AT:92m2 y un AC:113m2, cuenta con 3 iluminadas habitaciones, 3 baños, cochera, de estreno, con áreas verdes, piscina, cancha de fútbol, salones para actividades. ¡Para más información contactarse con nuestros agente!ID: 35751</Descripcion>
<Foto1>http://www.alfredograf.com/fotoprop/mini-35751%20-%201.jpg</Foto1>
<Foto2>http://www.alfredograf.com/fotoprop/mini-35751%20-%202.jpg</Foto2>
<Foto3>http://www.alfredograf.com/fotoprop/mini-35751%20-%203.jpg</Foto3>
<Foto4>http://www.alfredograf.com/fotoprop/mini-35751%20-%204.jpg</Foto4>
<Foto5>http://www.alfredograf.com/fotoprop/mini-35751%20-%205.jpg</Foto5>
<Foto6>http://www.alfredograf.com/fotoprop/mini-35751%20-%206.jpg</Foto6>
<Foto7>http://www.alfredograf.com/fotoprop/mini-35751%20-%207.jpg</Foto7>
<Foto8>http://www.alfredograf.com/fotoprop/mini-35751%20-%208.jpg</Foto8>
</propiedades>
</dataroot>
I wanted to know how I can extract the Images because every images have different XML name . like Fotos1,2,3 etc
Upvotes: 2
Views: 97
Reputation: 1109
You can access children by using children()
Try below code:
<?php
$s =<<<EOS
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="propiedades.xsd" generated="2015-04-28T15:16:46">
<propiedades>
<Descripcion>Excelente casa de dos pisos en condominio en la urbanización Santa Maria De Villa Club, en Carabayllo, con un AT:92m2 y un AC:113m2, cuenta con 3 iluminadas habitaciones, 3 baños, cochera, de estreno, con áreas verdes, piscina, cancha de fútbol, salones para actividades. ¡Para más información contactarse con nuestros agente!ID: 35751</Descripcion>
<Foto1>http://www.alfredograf.com/fotoprop/mini-35751%20-%201.jpg</Foto1>
<Foto2>http://www.alfredograf.com/fotoprop/mini-35751%20-%202.jpg</Foto2>
<Foto3>http://www.alfredograf.com/fotoprop/mini-35751%20-%203.jpg</Foto3>
<Foto4>http://www.alfredograf.com/fotoprop/mini-35751%20-%204.jpg</Foto4>
<Foto5>http://www.alfredograf.com/fotoprop/mini-35751%20-%205.jpg</Foto5>
<Foto6>http://www.alfredograf.com/fotoprop/mini-35751%20-%206.jpg</Foto6>
<Foto7>http://www.alfredograf.com/fotoprop/mini-35751%20-%207.jpg</Foto7>
<Foto8>http://www.alfredograf.com/fotoprop/mini-35751%20-%208.jpg</Foto8>
</propiedades>
</dataroot>
EOS;
$xml = simplexml_load_string($s);
foreach ($xml->propiedades as $element) {
foreach($element->children() as $key => $val) {
echo "{$key}: {$val}";
}
}
?>
Upvotes: 2
Reputation: 41885
You can use an xpath query to target the node names that starts with Foto
, then after getting those nodes, then just use foreach
:
$xml_string = '<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="propiedades.xsd" generated="2015-04-28T15:16:46">
<propiedades>
<Descripcion>Excelente casa de dos pisos en condominio en la urbanización Santa Maria De Villa Club, en Carabayllo, con un AT:92m2 y un AC:113m2, cuenta con 3 iluminadas habitaciones, 3 baños, cochera, de estreno, con áreas verdes, piscina, cancha de fútbol, salones para actividades. ¡Para más información contactarse con nuestros agente!ID: 35751</Descripcion>
<Foto1>http://www.alfredograf.com/fotoprop/mini-35751%20-%201.jpg</Foto1>
<Foto2>http://www.alfredograf.com/fotoprop/mini-35751%20-%202.jpg</Foto2>
<Foto3>http://www.alfredograf.com/fotoprop/mini-35751%20-%203.jpg</Foto3>
<Foto4>http://www.alfredograf.com/fotoprop/mini-35751%20-%204.jpg</Foto4>
<Foto5>http://www.alfredograf.com/fotoprop/mini-35751%20-%205.jpg</Foto5>
<Foto6>http://www.alfredograf.com/fotoprop/mini-35751%20-%206.jpg</Foto6>
<Foto7>http://www.alfredograf.com/fotoprop/mini-35751%20-%207.jpg</Foto7>
<Foto8>http://www.alfredograf.com/fotoprop/mini-35751%20-%208.jpg</Foto8>
</propiedades>
</dataroot>';
$xml = simplexml_load_string($xml_string);
$fotos = $xml->xpath('//*[substring(name(), 1, 4) = "Foto"]');
foreach($fotos as $foto) {
echo $foto, '<br/>';
}
If you don't want to use the xpath route, just use ->getName
to check the node name and use normal PHP string functions to check it:
$xml = simplexml_load_string($xml_string);
foreach($xml->propiedades->children() as $element) {
if(substr($element->getName(), 0, 4) === 'Foto') {
echo $element, '<br/>';
}
}
Upvotes: 2
Reputation: 2528
Yes you can do that,
$xml = simplexml_load_string($string);
put your whole xml content in string,
and use reference from here http://www.w3schools.com/php/func_simplexml_load_string.asp
Upvotes: 1