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

    xml is just text, why would you want to put it in a blob (binary) field?

      First, thanks dagon for taking a look and giving me the URL to XML functions. I will study it shortly.

      Second, you ask a rational and reasonable question. I have not programmed in a long time. I know that XML is a text file, but I know little about PHP and how many of its functions behave. I am guessing how PHP treats the XML file because I am confused by the following results.

      • After I open the file with “$xml = simplexml_load_file('http:\url\file.xml'),” the PHP function “is_object($XML)” returns a Boolean true. I (perhaps ignorantly) assume that an object is not text.
      • The “strlen($xml)” function returns a length of 3. When I insert $XML (as an object) into a text field, I see 3 little boxes in the text field that remind me of an ASCII representation of a linefeed. When I printf it, I seem to get linefeeds.
      • After I use the “serialize($xml)” function, the strlen() function returns a length of about 40K. When I insert the serialized $XML into the text field, I get a nice long text string, but it is no longer XML. Unserializing it does not seem to reverse the process.

      It would be great to simpley drop $XML (as is) into a text field and subsequently read it (as is). Soooo… what am I missing, dagon? I am obviously confused. But if your picture is an example of what might happen to me when I eat PHP for breakfast... I believe that I have something to look forward to.

        Never mind. I think solved it.
        I used file_get_contents() to read the file as a string (instead of as an array with file() or an object with simplexml_load_file()), which makes it easy to insert it into a field because it is not an object.

        Then I use simplexml_load_string() when I select the field. This makes it easy to process it as well-formed xml rather than an array or object.

          Write a Reply...