Link to home
Start Free TrialLog in
Avatar of Heather Ritchey
Heather RitcheyFlag for United States of America

asked on

Dynamic XML node creation with php

I had to create an xml export using php pulling the info from a database. The last section that needs done adds in node pieces that could range anywhere up to 20 additions.

I can extract the data I need from the database with no problem, but I don't know how to create these sub & sub-children "on-the-fly". By "on-the-fly" I mean that there could be two additions, or there could be up to 20.

This is my placeholder code for where this needs to be done:
	// PHOTOS SECTION NEEDS DONE
	$node_photos = $doc->createElement("photos");
	$node->appendChild($node_photos);
	$newnode2 = $doc->createElement("photo");
	$node_photo = $node_photos->appendChild($newnode2);
	$node_photo1 = $doc->createElement("url", 'THIS SECTION NEEDS DONE. NEED TO CYCLE THROUGH AND CREATE NODES ON THE FLY.');
	$node_photo->appendChild($node_photo1);

Open in new window


This is the example result that has to display:
		<photos>
			<photo>
				<url>http://doesmobilehomes.com/photos/24/1.jpg</url>
				<title>front view of the home</title>
			</photo>
			<photo>
				<url>http://doesmobilehomes.com/photos/24/2.jpg</url>
				<title>kitchen</title>
		        </photo>
                </photos>

Open in new window


It may or may not be simple to do this, but I've never used the php code for producing xml up until now, so I'm not sure how to go about this. The photo info (as you can see in the example is what I'm adding in) I can pull that into arrays and iterate through. But I don't understand how to code the add node parts as it iterates.

This is the full display at this point: http://www.manufacturedhomesoregon.net/xml-export/xml-export-test.php
So you can see where the photo info section displays in the output.

Any help is appreciated. I can provide more info if needed.
Thanks, Heather
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Heather Ritchey

ASKER

ok, so re-using $node_photo1 over and over will still add them all? I made the assumption that was a variable that had to have a new name each time.
No, there is no need to change the variale name: simply its content will change each time :)
Thank you very much. Again - "learn something new every day"!