Remove XML Elements using PHP Quick and Painless

27 February

Remove XML Elements using PHP Quick and Painless

Let's just begin by stating that XML is not my forte. Ok, with that out the way lets get on with it. So recently my challenge was to grab a file from a 3rd party server that was CSV. No harm no foul right? well not when the 3rd party site had to be connected from a secure FTP. So after rummaging around the web I found (phpseclib0.2.1a) which worked great to connect and download the CSV file I needed. Then I took that CSV (because let's face it - nobody really likes CSV) and converted it into XML.

Ok?! so what right?

Well the data that is coming in a hodge-podge of information which I didn't really need. I only needed a certain part of the XML file. So what was i suppose to do?

After a bunch of trial and error, internet help and so on and so forth, here is the solution I came up with:

First let's just assume that your XML file looks something like this:


    	00001
      	Some Name 1
      	1

    	00002
      	Some Name 2
      	2

    	00003
      	Some Name 3
      	1

    	00004
      	Some Name 4
      	2

Now in this example lets assume that do_you_need_me = 0 (meaning you DON'T need it) and obviously do_you_need_me = 1 (meaning you DO need it) .

There is alot of information out there some that might work, some that might not work. This is how i was able to deal with this and have it work.

This code will irrerate through the XML file and select EVERYTHING that is NOT a category of 1 (category = 1) and delete those from the XML file and of course saves the XML file again.

Here is the complete function that I used to make this happen.

$file_xml = "/path/to/your/xml/file.xml";
$data = simplexml_load_file($file_xml);
$data_count	= count($data->row);
for($i = 0; $i row[$i]->category == "1")
    {
        unset($data->row[$i]);
    }
    else
    {
		// I just like keeping the else in here. just in case.
    }
}
file_put_contents($file_xml, $data->saveXML());

Quick Synopsis

I have left the comments that I tested with in there so you can test if you want, just make sure you comment out unset($data->row[$i]);

So $file_xml is self explanitory.
we then loop through the XML file and if the current rows do_you_need_me != 1 then we go ahead and delete the record. Be mindful on the size of your XML file. If its too large a file, I would maybe suggest taking "file_put_contents($file_xml, $data->saveXML());" and moving it under the "unset($data->row[$i]);" to save the file after every run.

If you are running this through a cron, first check if your server allows you to override the time with "set_time_limit(0);" if they dont, then you might want to move that "file_put_contents($file_xml, $data->saveXML());" as explained above and run the cron a couple times in a row just to be certain that all the information you need has been parsed.

If you have anything to add or comment about please do so. If there is an easier way to handle this, I would love to know.

TAGS: Web Development, Social Media