I would like to put an XML file in a blob field and retrieve it for later processing. Someone told me that I have to serialize it first before I insert it. If I serialize it, then it is no longer an XML object. I was hoping that I could just parse the XML using XPath and other xml functions without having to copy the field to a variable and then parsing it.
1. Do I really need to serialize it to put it into a blob field?
2. Is there a way to parse the field as though it were a variable? If so, how?
3. If I do need to serialize it, how do I convert it back? Whatever I am doing with unserialize does not seem to work.
The xml file averages 40K each. Here is the code.
<?php
$xml = simplexml_load_file('http:\url\file.xml');
$mysqli = new mysqli("localhost", "name", "password", "database");
/ check connection /
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$xml = serialize($xml) ;
// It seems to insert o.k. as a string.
$stmt = $mysqli->prepare("insert into table (XML_Field) values(?)");
$stmt->bind_param('s', $xml);
$stmt->execute();
// I don't have any luck getting it out.
$result = $mysqli->query("SELECT XML_Field FROM table " .
"WHERE (PrimaryKey)=9");
while ($obj = $result->fetch_object()) {
printf ("%s \n", $obj-> $rrr = $obj->XML_Field);
}
// Gives me -- (O:16:"SimpleXMLElement" and a lot of stuff afterwards.
$rrr = unserialize($rrr);
echo "object: " . is_object($rrr) . "\n"; // gives me null.
?>
Any thoughts? Thanks, Michael