Hello all,

I am getting the following error message:

Warning: DOMDocument::createTextNode() expects parameter 1 to be string, object given in ...

The complete code that generate the above error is:

function save_document_info($fileInfo) {
$doc = new DOMDocument('1.0');
$root = $doc->createElement('workflow');
$doc->appendChild($root);

$statistics = $doc->createElement("statistics");
$statistics->setAttribute("total", "1");;
$statistics->setAttribute("approved", "0");;
$root->appendChild($statistics);

$filename = $fileInfo['name'];
$filetype = $fileInfo['type'];
$filesize = $fileInfo['size'];

$fileInfo = $doc->createElement("fileInfo");

$fileInfo->setAttribute("status", "pending");
$fileInfo->setAttribute("submittedBy", $_SESSION["username"]);

$approvedBy = $doc->createElement("approvedBy");

$filename = $doc->createElement("fileName");
$fileNameText = $doc->createTextNode($filename);
$filename->appendChild($fileNameText);

$location = $doc->createElement("location");
$locationText = $doc->createTextNode(UPLOADEDFILES);
$location->appendChild($fileNameText);

$type = $doc->createElement("fileType");
$typeText = $doc->createTextNode($filetype);
$type->appendChild($typeText);

$size = $doc->createElement("size");
$sizeText = $doc->createTextNode($filesize);
$size->appendChild($sizeText);

$fileInfo->appendChild($approvedBy);
$fileInfo->appendChild($filename);
$fileInfo->appendChild($location);
$fileInfo->appendChild($type);
$fileInfo->appendChild($size);

$root->appendChild($fileInfo);

$doc->save(UPLOADEDFILES."docinfo.xml");

}

and the code that calls the above function is:

save_document_info($_FILES['ufile']);

ufile is the name of the form element used to upload the file.

Your help is very much appreciated.

Asfaw

    In a couple of places you do something like this:

    $filename = $doc->createElement("fileName");
    $fileNameText = $doc->createTextNode($filename);
    

    But the createElement() method returns a DomElement object, while the createTextNode() method expects a string as its argument. As I'm not sure what your exact intent is, I'm not sure what the fix is.

      <code>
      $filename = $doc->createElement("fileName");
      $fileNameText = $doc->createTextNode($filename);
      </code>

      The first line is to create the xml element called file name and the second is to create the text node of file name to store the name of the file.

      The purpose of this application is to store file information (file name, size, user name, file type, etc to an xml file). When a user uploads a file we would like to store the file information to an xml file.

      Hope this clarifies our intent.

      Asfaw

        So in the line

        $fileNameText = $doc->createTextNode($filename); 

        Instead of the DOMElement $filename, you want the text that is the name of the file - wherever you get that from. And then use appendChild to make $fileNameText a child of the $filename element.

          How can I get "the text that is the name of the file"?

          Regards,

          Asfaw

            Um, isn't that $fileInfo['name']?

            (Basically, look at your code: you're using the same variable twice for two completely different things and getting yourself confused about what's in it.)

              Write a Reply...