Okay, what should be a simple little task has already cost me three days! I am working with PHP on OS X 10.6.8 and for various reasons cannot upgrade to Mavericks, so I am stuck without a proper PHP IDE and damned to TextWrangler.
So here's my XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<adapt_xml>
<annonce>
<reference>0001</reference>
<liste_photos>
<photo>photo1.jpg</photo>
<photo>photo2.jpg</photo>
<photo>photo3.jpg</photo>
<photo>photo4.jpg</photo>
</liste_photos>
<agence>AVENUE RIVIERA</agence>
</annonce>
<annonce>
<reference>0002</reference>
<liste_photos>
<photo>photo1.jpg</photo>
<photo>photo2.jpg</photo>
<photo>photo3.jpg</photo>
<photo>photo4.jpg</photo>
</liste_photos>
<agence>AVENUE RIVIERA</agence>
</annonce>
<annonce>
<reference>0003</reference>
<liste_photos>
<photo>photo1.jpg</photo>
<photo>photo2.jpg</photo>
<photo>photo3.jpg</photo>
<photo>photo4.jpg</photo>
</liste_photos>
<agence>AVENUE RIVIERA</agence>
</annonce>
</adapt_xml>
I have figured out how to cycle through the annonce elements to extract each sub attribute so that I can write them all to my database table annonce:
$xmlFile = simplexml_load_file($file);
foreach($xmlFile->annonce as $annonce)
{
foreach($annonce->children() as $child)
{
...
echo $child->getName().": ".(string)$child."<br />";
}
}
What I want to be able to do is cycle through the photos, recuperating each photo as well as the referent to the annonce (the relationship photo to annonce is many-to-one). When I insert the code into the foreach($annonce->children() as $child)
space, nothing shows up for the photos:
$xmlFile = simplexml_load_file($file);
foreach($xmlFile->annonce as $annonce)
{
foreach($annonce->children() as $child)
{
...
echo $child->getName().": ".(string)$child."<br />";
foreach ($child->{"liste_photos"} as $liste_photos)
{
foreach ($liste_photos->{"photo"} as $photo)
{
...
echo $photo->getName().": ".(string)$photo."<br />";
}
}
}
}
What am I doing wrong??