php - Export Array of SimpleXMLElement objects to MySQL Database

735

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?

800

Answer

Solution:

You have to iterate through your array to create a new array fulfilled with strings instead of SimpleXMLElement, such as :

<?php

// your array (already built)
$arraySimpleXml = array(
                    "example1" => new SimpleXMLElement("<test>value</test>"),
                    "example2" => new SimpleXMLElement("<test>value2</test>")
                  );

// output array, to store in database
$result = array();

foreach($arraySimpleXml as $key => $simpleXml) {
  $result[$key] = $simpleXml->asXML();
}

// gets your result as a string => you can now insert it into mysql
$dbInsertion = serialize($result);

?>

716

Answer

Solution:

So I worked out how to change the data into a standard array rather than an array ofSimpleXMLElements so that I can successfully insert it into a MySQL database.

When iterating theSimpleXMLElement 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):

Array
(
[0] => Array
    (
        [uid] => WOS:000238186400009
        [journal] => JOURNAL OF ZOOLOGY
        [publication] => Abundance of hedgehogs (Erinaceus europaeus) in relation to the density and distribution of badgers (Meles meles)
        [year] => 2006
        [author1] => Young, RP
        [address] => Cent Sci Lab, Sand Hutton, Yorks, England
        [author2] => Davison, J
        [author3] => Trewby, ID
        [citations] => 15
    )
)

Thought I'd post this in case anyone has a similar problem in future. To do this, when iterating the data instead of:

$uid = $record->UID;

I did:

$uid = (string)$record->UID;

For each of the data fields I required. This ensures the data is stored as a String and so removes theSimpleXMLElement format.

People are also looking for solutions to the problem: php - Protection against tampered user input and SQL injection fails

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.