Arrays to XML - Does not make sense!

Hi devs,

I have a small challenge that I can’t seem to solve or understand. Please give me some hints or advice.

In the code below I have an array with minimal data. When I run a recursive loop through the array it only prints the last child element in the array’s hierarchy. If I use this same algorithm in Java for instance it works as expected but not in PHP…


class ExportArrayToXML {
	
	var $file;
	var $handler;
	
	function __construct() {
		$this->file = "XML_DOC.xml";
		$this->handler = fopen($this->file, 'w') or die("can't open file");
		
		$dataArray = array(
			'root' => array( //only root and the last of it's child element is stored
				'post' => 'POST_1',
				'post' => 'POST_2',
				'post' => 'POST_3' //only the last element is stored
			)
		);

		$this->exportToXML($dataArray);

		fclose($this->handler);
	}
	
	function exportToXML($data) {
		if(is_array($data)) {
			foreach($data as $element=>$key) {
				fwrite($this->handler, "<$element>");
				
				if(is_array($key)) {
					$this->exportToXML($key);
				} else {
					fwrite($this->handler, $key);
				}
				
				fwrite($this->handler, "<$element>");
			}
		} else {
			fwrite($this->handler, $data);
		}
	}
}

$eatx = new ExportArrayToXML();

The results in the saved file is as follows:

<root><post>POST_3<post><root>

I expect it to be:

<root><post>POST_1<post><post>POST_2<post><post>POST_3<post><root>

I appreciate any assistance or feedback.

There’s nothing wrong with the loop. The problem is with the array declaration in the assignment to $dataArray. Basically arrays in PHP are like maps so if you associate more than one value with any particular key—in this case ‘post’—that key will contain the last element associated.

Also consider changing the name of the variable $key in your foreach loop. Although you use them correctly it is not correct to refer to the variable declaration after the => operator as a “key”. Consider something like content; body; subElements; children; or—if you’re a funky guy such as myself—simply, subs.

That is definitely explaining why and it’s so obvious… Thank you.

Do you have any suggestions as to what I can use to accomplish the following task?

I want to push data into an array and based on the hierarchy of the array, save it to an XML file. The array keys would represent the elements in the XML doc. I have over 5000+ blog posts that I want to export to XML files but I need to include additional data in it as well for each different post type so it’s not just simply exporting the data. I need to include data from various MySQL tables. The way PHP arrays can be structured is perfect for what I want to do but the quoted text explains why it can’t be done.

The way PHP arrays can be structured is perfect for what I want to do but the quoted text explains why it can’t be done.
Well it is true you can’t have multiple keys of the same name but you can have a sequence of arrays each using a key of the same name:

$aList = array(
   array('post' => 'foo'),
   array('post' => 'bar')
);
foreach ($aList as $elem) {
   list($name, $value) = $elem;
   echo "<$name>$value</$name>"; }

But all this is moot for your question I think because you aren’t likely to construct an array such as that in the first place.

Let me ask you this: how are you getting the data you need to export? My guess is that it will be from a database. If that’s so why don’t you generate the XML the same way everyone else generates HTML from a database, namely: queries and row fetches within various loops while echoing appropriate output. If you want to you could use DOMDocument class and make repeated calls to appendChild() etc. which would guarantee well-formedness but you could achieve the same thing by being vigilant with your loops and echos.

The data does come from a database. I was trying to see if one could populate an array into the correct structure and only write a script to convert that array to an XML doc with the keys as elements. I know now that it would not be practical in doing so because the amount of data will probably be to much for an array and it will consume memory and CPU usage. I am currently working on a script to connect to the db and do it like you suggested. I do appreciate your feedback. Thank you.

The data does come from a database. I was trying to see if one could populate an array into the correct structure and only write a script to convert that array to an XML doc with the keys as elements.
Yes, I got that impression. Why do you need an intermediary representation though? One possible reason might be that you want to know the whole number of elements of something ahead of time or something like that but you haven’t said as much, and that work would probably more easily be done with queries anyway. This is why I suggested not having any intermediary representation and just looping through things. Let me know if you would like a fuller explanation of that suggestion.

I know now that it would not be practical in doing so because the amount of data will probably be to much for an array and it will consume memory and CPU usage.
Do you have to do all 5000 in one go?

I’m probably going to need more information on the structure of the input you have and output you want is if I’m going to be able to help you further.

This is what I need to do: I have a WordPress multi-site installed on one of my domains. That multi-site consists of about 6 different sites full of information. I want to combine all of those sites into one single WordPress installation. The challenge that I have is to export all of the posts, pages and custom post types of each of those sites to files as well as saving the images that was used for each post. I then need to remove the multi-site, install a single WordPress site and have all of that data imported into the new site. The 5000+ records is just for one of those sites, some of the other sites have more than 20000 records. I basically need to try and make the exporting and importing of the data seamless as I have one chance to make the switch. We are an online food and restaurant magazine and cannot really afford to have the site down for longer than one night (GMT+2). I am aware that the importing of the data will take a lot of time thus I’m making enough time to correct errors along the way during the importing process.

"This is what I had in mind: I am writing a script now to loop through the database tables to get all the records of posts and the related information like categories, tags and authors. I then want to save all of that info into XML files and the images in the same location. This process I want to repeat for all of the sites. The different sites use custom post types like recipes, products, listings etc. When I do the importing I need to replace all the images in the articles with it’s new locations on the site.

I have found a pretty good import plugin for WordPress that allows me to import XML files into the site and it also allows me to upload the images to the media library. If this does not give me the desired results I will need to code an import script to do it myself. "

I had a look at a few of my options regarding exporting and importing data and the default WordPress export and import will not work. I have already tested it on a demo site and it keeps on giving me the results that I am not looking for. For eg. it imports menus and widgets which is not what I want as I am rebuilding that on the new single site. If I only export posts and pages the images do not get imported into the media library. I have tried many other plugins as well but in most cases I got the same results.

If you know of any other alternative way to do this please let me know. I am writing my own scripts to do the exporting and importing for now but if there is another way to get this done I would love to know how and will test it on a demo site as well.

I have a WordPress multi-site installed on one of my domains. That multi-site consists of about 6 different sites full of information. I want to combine all of those sites into one single WordPress installation. The challenge that I have is to export all of the posts, pages and custom post types of each of those sites to files as well as saving the images that was used for each post.

Ah OK, gotcha. I’m gonna ignore the images issue for now and focus on the main issue of creating an XML file from these 6 sites.

The 5000+ records is just for one of those sites, some of the other sites have more than 20000 records. I basically need to try and make the exporting and importing of the data seamless as I have one chance to make the switch.
The quantity of records increases the difficulty of the task moderately but not massively so don’t worry too much about that. When we start out it’ll probably be a good idea to put LIMIT clauses on all the queries so we’re only working with a small bit of data at a time. Making a relatively seamless switch shouldn’t be too difficult either provided you keep the original versions running until you’ve got a fully functioning new site.

I am aware that the importing of the data will take a lot of time thus I’m making enough time to correct errors along the way during the importing process.
A lot of machine time or a lot of programmer time? Because I don’t think this will take that much machine time; maybe 10 minutes if you’re unlucky. You might want to make sure your webhost is OK with you hogging the CPU while you do this though. Some have settings that will kill any process that runs for more than 20 seconds or so but if you explain your situation they may temporarily remove that restriction.

I have found a pretty good import plugin for WordPress that allows me to import XML files into the site and it also allows me to upload the images to the media library. If this does not give me the desired results I will need to code an import script to do it myself.
OK so what are the names of the tables and their fields you will be querying from? And how should the XML output be formatted? (Those are the most important questions).

If you know of any other alternative way to do this please let me know. I am writing my own scripts to do the exporting and importing for now but if there is another way to get this done I would love to know how and will test it on a demo site as well.
Perhaps you could ask on Stack Overflow how someone with significant Wordpress experience would approach this problem. I don’t know Wordpress, sorry. If you’re happy to proceed with the course of action you are describing (creating this XML file) then I can help you with that because I know programming and problem solving pretty well.