0

I'm parsing and XML file with a list of a Real Estate buildings. All is getting fine, I can take each node and create a separate wordpress post with custom infos. The problem is when I try to access to the images node: every building has an image node, with multiple images in it. This is what I'm doing:

XML STRUCTURE

<annunci> <annuncio ..blablabla ...> ...some stuff... <immagine categoria="PLANIMETRIE" didascalia="img_5862" tipo="foto">659-{23d7ac25-549e-40ef-9cb2-1e6f687182ec}_0.jpg</immagine> <immagine categoria="PLANIMETRIE" didascalia="img_5864" tipo="foto">659-{8b9c3da4-edde-489a-8ca9-3dc91a0c5063}_1.jpg</immagine> <immagine categoria="PLANIMETRIE" didascalia="img_5868" tipo="foto">659-{57790b4a-2482-4a66-8f7f-1a11765d1cab}_2.jpg</immagine> <immagine categoria="PLANIMETRIE" didascalia="img_5870" tipo="foto">659-{b28b62f7-6375-4592-a269-c4ac8eac6087}_3.jpg</immagine> <immagine categoria="PLANIMETRIE" didascalia="img_5871" tipo="foto">659-{0bf70101-b0ec-4835-9a06-3b916ad21241}_4.jpg</immagine> <immagine categoria="PLANIMETRIE" didascalia="img_5872" tipo="foto">659-{dc5f87a1-364a-4c05-9a3a-be4cb51016f9}_5.jpg</immagine> <immagine categoria="PLANIMETRIE" didascalia="img_5875" tipo="foto">659-{2fe1524d-b454-423c-90bf-a6f9fff17e8d}_6.jpg</immagine> <immagine categoria="PLANIMETRIE" didascalia="img_5876" tipo="foto">659-{1060d120-b339-4bab-89cd-6b7781143a74}_7.jpg</immagine> <immagine categoria="PLANIMETRIE" didascalia="img_5882" tipo="foto">659-{191c77d7-76f5-4fb9-873c-9221f755dcba}_8.jpg</immagine> <immagine categoria="PLANIMETRIE" didascalia="planimetria" tipo="foto">659-{14a9a6f0-dbf5-48a2-a204-976b9a146f43}_9.jpeg</immagine> ...some other stuff ... </annuncio> </annunci> 

And this is the code I use in my plugin:

 if (!$xml = simplexml_load_file($xml_path)) { echo 'unable to load XML file'; } else { ... foreach ($xml->annuncio as $annuncio) { ... $immagini = (array)$annuncio->immagine; ... echo '<pre>'; //THIS IS FOR DEBUGGING print_r($immagini); echo '</pre>'; } 

And I get this kind of array, one for each building I'm parsing:

Array ( [@attributes] => Array ( [categoria] => PLANIMETRIE [didascalia] => img_5862 [tipo] => foto ) [0] => 659-{23d7ac25-549e-40ef-9cb2-1e6f687182ec}_0.jpg [1] => 659-{8b9c3da4-edde-489a-8ca9-3dc91a0c5063}_1.jpg [2] => 659-{57790b4a-2482-4a66-8f7f-1a11765d1cab}_2.jpg [3] => 659-{b28b62f7-6375-4592-a269-c4ac8eac6087}_3.jpg [4] => 659-{0bf70101-b0ec-4835-9a06-3b916ad21241}_4.jpg [5] => 659-{dc5f87a1-364a-4c05-9a3a-be4cb51016f9}_5.jpg [6] => 659-{2fe1524d-b454-423c-90bf-a6f9fff17e8d}_6.jpg [7] => 659-{1060d120-b339-4bab-89cd-6b7781143a74}_7.jpg [8] => 659-{191c77d7-76f5-4fb9-873c-9221f755dcba}_8.jpg [9] => 659-{14a9a6f0-dbf5-48a2-a204-976b9a146f43}_9.jpeg ) 

First of all, I can't get a rid of 's attribute, and, I always get an error when I try to access to single images like

echo $immagini[0]; 

I'm pretty sure is something stupid, but I cannot find a solution, can someone help me?

EDIT 1 I'm sorry, the error is when I try to access to single images in loop:

foreach($immagini as $test) { echo $test; } 

I got Notice: Array to string conversion in ...

EDIT 2 I solved with a FOR loop, but still don't understand :D Anyway, it works!

1 Answer 1

1

Man,

I hope will take a best experience with DOMDocument then SimpleXML.

When working with complex XML data, the solution may not be "Simple", then you may not use SimpleXML to do that things.

Work with DOMDocument, I take solved complex issues with booking tickets for airline companies using DOMDocument.

<?php $dom = new DOMDocument(); $dom->loadXML('/path/to/xml.xml'); foreach ($dom->getElementsByTagName('annuncio') as $annuncio){ foreach ($annuncio->getElementsByTagName('immagine') as $immagine){ $categoria = $immagine->getAttribute('categoria'); } } ?> 

Enjoy DOM!

Sign up to request clarification or add additional context in comments.

1 Comment

You're right man, but this was the first time I had to parse an XML document :D I had to work with a sample-easy-almost-empty xml file, and today I received the big-complex-one! I contuned my work as I started (with simplexml) but now I think I'll change it to DOM! Thank you for reply, and have a nice day ^^

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.