0

I have followed this example and created a php file to write into an xml file config.xml. But for some reason, when I'm trying to see what is written in my xml file, I don't see anything. Here's my code -

    $doc=new DOMDocument("1.0");
    //load the file
    $doc->load('localpath/config.xml');
    echo 'Start writing the xml file1';

    //create chapter-element
    $porpoiseconfiguration=$doc->createElement('porpoise-configuration');
   //create title-element
   echo 'Start writing the xml file2';

   $developerid=$doc->createElement('developer-id');
   //insert text to the title
   $developerid->appendChild($doc->createTextNode('00'));

   $developerkey=$doc->createElement('developer-key');
   //insert text to the title
   $developerkey->appendChild($doc->createTextNode('00'));
   echo 'In the 1st middle writing3';

   $connectors=$doc->createElement('connectors');
   $connector=$doc->createElement('connector');
   $name1=$doc->createElement('name');
   $name1->appendChild($doc->createTextNode('Silverspring'));
   $file=$doc->createElement('file');
   $file->appendChild($doc->createTextNode('silverspringpoiconnector.class.php'));
   echo 'In the 1st middle writing4';

   $connector->appendChild($name1);
   $connector->appendChild($file);
   $connectors->appendChild($connector);
   echo 'In the 1st middle writing5';

   $layers=$doc->createElement('layers');
   $layer=$doc->createElement('layer');
   $name2=$doc->createElement('name');
   $name2->appendChild($doc->createTextNode('gamename'));
   $source=$doc->createElement('source');
    $dsn=$doc->createElement('dsn');
   $dsn->appendChild($doc->createTextNode("mysql:host=myhost;dbname='$dbname'"));
    $username=$doc->createElement('username');
   $username->appendChild($doc->createTextNode('myusername'));
    $password=$doc->createElement('password');
   $password->appendChild($doc->createTextNode('mypass'));

   $source->appendChild($dsn);
   $source->appendChild($username);
   $source->appendChild($password);
   $layer->appendChild($source);
   $layer->appendChild($name2);

   $connector2=$doc->createElement('connector');
   $connector2->appendChild($doc->createTextNode('SilverspringPOIConnector'));
   echo 'In the 1st middle writing6';

   $layers->appendChild($layer);
   $layers->appendChild($connector2);
   echo 'In the 1st middle writing7';
   $porpoiseconfiguration->appendChild($developerid);
   $porpoiseconfiguration->appendChild($developerkey);
   $porpoiseconfiguration->appendChild($connector);
   $porpoiseconfiguration->appendChild($layers);
   echo 'In the 1st middle writing8';
   $doc->documentElement->appendChild($porpoiseconfiguration);

   //$doc->documentElement->appendChild($chapter);

    echo 'done writing';
    echo $doc->saveXML();
    $doc->save('localpath/config.xml');

I have checked it with echos and there is no syntax error. Can anyone please tell me what's I'm doing wrong here?

EDIT::::::::::::

I'm getting error - Fatal error: Call to a member function appendChild() on a non-object in /var/www/html/PasswARGUI1/myWriteXML.php on line 103
It is indicating this line - $doc->documentElement->appendChild($porpoiseconfiguration);

Please Help :(

1
  • Ok, I have edited accordingly, changed the file permissions .. still not working :( Commented Jun 27, 2011 at 18:49

1 Answer 1

2
$doc->save('http://myServer/config.xml');

you can't write to a http:// location, the http wrapper doesn't support it.

Use a local file path instead.

Sign up to request clarification or add additional context in comments.

4 Comments

OK, I have tried to just use the file name (since for now they are in same directory) but still it is not working. I put an echo after save, that echo is not also printing :(
Actually, well, my folder did not have write permission on it. I have changed it and the last echo 'done saving' is also being printed. But still nothing is written in that file :(. One more thing. Should there be something written "<?xml version="1.0"?>" in that file? I have not written anything like that and no idea how to write though :(
I added $doc-> createProcessingInstruction("xml", " version='1.0'"); line as well
@Pow that is really strange. Try adding error_reporting(E_ALL); to the document's first line, see whether any errors come up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.