simplexml_load_file array keys

Hi all,

I have several large xml files containing data that I intend to put into MySQL. I’ve constructed MySQL tables, matching the structure of each XML file.

The XML files will update daily, and therefore I intend to wipe and then re-populate each table daily.

I intend on doing this by setting up a daily cron job to execute a php script which wipes the existing tables, loops through the XML and inserts it. The bit I’m struggling with is creating the insert commands as I loop through the XML. Realistically, I need the MySQL insert statements to be generated automatically, because although I could type the insert commands out, if new xml files are introduced in the future, I won’t have the time.

The base code:

<?php
#Simple XML
$xml = simplexml_load_file("../xml/invoices.xml");

$c=0;
$count = count($xml->invoice_record);


foreach ($xml->invoice_record as $prop) {
		// INSERT query built and executed here
	$c++;
}
?>

If I could loop through the various array key names of $prop, then I could automatically build the insert statement. For some reason this doesn’t work. I’ve tried using the key($array) function to no avail.

Any help would be appreciated,

Does this help any?
http://dev.mysql.com/doc/refman/5.5/en/load-xml.html

I would think it would be easy enough to schedule a cron job to load an SQL file with a couple of TRUNCATE commands followed by this LOAD XML stuff.

If you’re trying to get the name of an XML node (e.g. foo of <foo>) then you can use the [font=monospace]SimpleXMLElement::getName[/font] method on $prop.

If that’s not your problem, I’m not sure what is. Whether you know it or not, your description is not helping us to help you (there are no arrays at all!).

If you’re trying to get the name of an XML node (e.g. foo of <foo>) then you can use the SimpleXMLElement::getName method on $prop.

This is exactly what I was looking for. Please excuse my ignorance. Thank you!

Does this help any?
http://dev.mysql.com/doc/refman/5.5/en/load-xml.html

I would think it would be easy enough to schedule a cron job to load an SQL file with a couple of TRUNCATE commands followed by this LOAD XML stuff.

This may also come in handy, many thanks.