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 aSimpleXMLElement
. I have then iterated through this object to extract the various fields I need and save them into an array, which becomes an array ofSimpleXMLElement
objects.
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 usingmysql_query("INSERT INTO (whatever) VALUES (whatever)");
. I have triedimplode
andserialize
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 theSimpleXMLELement
looks like:
Array
(
[0] => Array
(
[uid] => SimpleXMLElement Object
(
[0] => WOS:000238186400009
)
[journal] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => source
)
)
[publication] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => item
)
[0] => Abundance of hedgehogs (Erinaceus europaeus) in relation to the density and distribution of badgers (Meles meles)
)
[year] => 2006
[author1] => SimpleXMLElement Object
(
[0] => Young, RP
)
[address] => SimpleXMLElement Object
(
[0] => Cent Sci Lab, Sand Hutton, Yorks, England
)
[author2] => SimpleXMLElement Object
(
[0] => Davison, J
)
[author3] => SimpleXMLElement Object
(
[0] => 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?
Answer
Solution:
You have to iterate through your array to create a new array fulfilled with strings instead of SimpleXMLElement, such as :
?>
Answer
Solution:
So I worked out how to change the data into a standard array rather than an array of
SimpleXMLElement
s so that I can successfully insert it into a MySQL database.When iterating the
SimpleXMLElement
object 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:
I did:
For each of the data fields I required. This ensures the data is stored as a String and so removes the
SimpleXMLElement
format.