php - Export Array of SimpleXMLElement objects to MySQL Database
I have a SOAP Response from a Web Service and have extracted the XML data as a
SimpleXMLElement. I have then iterated through this object to extract the various fields I need and save them into an array, which becomes an array of
I am now trying to export this data into a MySQL Database which, according to my research, means turning the array into a String and then using
mysql_query("INSERT INTO (whatever) VALUES (whatever)");. I have tried
serialize but neither work and I get the error:
Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'
This is what the array I have created from the
SimpleXMLELement looks like:
Array (  => Array ( [uid] => SimpleXMLElement Object (  => WOS:000238186400009 ) [journal] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => source ) ) [publication] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => item )  => Abundance of hedgehogs (Erinaceus europaeus) in relation to the density and distribution of badgers (Meles meles) ) [year] => 2006 [author1] => SimpleXMLElement Object (  => Young, RP ) [address] => SimpleXMLElement Object (  => Cent Sci Lab, Sand Hutton, Yorks, England ) [author2] => SimpleXMLElement Object (  => Davison, J ) [author3] => SimpleXMLElement Object (  => Trewby, ID ) [citations] => SimpleXMLElement Object ( [@attributes] => Array ( [local_count] => 15 [coll_id] => WOS ) ) ) ... etc ... )
Can anyone help me with the method to get this data into my database, please? Do I need to change it into (yet) another format?
You have to iterate through your array to create a new array fulfilled with strings instead of SimpleXMLElement, such as :
So I worked out how to change the data into a standard array rather than an array of
SimpleXMLElements so that I can successfully insert it into a MySQL database.
When iterating the
SimpleXMLElementobject to extract the data I needed I cast the type as String so that now my array has the format (as opposed to above):
Thought I'd post this in case anyone has a similar problem in future. To do this, when iterating the data instead of:
For each of the data fields I required. This ensures the data is stored as a String and so removes the