在服务器上用PHP编写XML文件

时间:2011-06-27 08:34:01

标签: php dom xml

我已经关注 this example并创建了一个php文件来写入xml文件config.xml。但出于某种原因,当我试图查看我的xml文件中写的内容时,我什么都看不到。这是我的代码 -

    $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');

我用回声检查了它,没有语法错误。谁能告诉我这里我做错了什么?

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

我收到错误 - Fatal error: Call to a member function appendChild() on a non-object in /var/www/html/PasswARGUI1/myWriteXML.php on line 103
这表明这一行 - $doc->documentElement->appendChild($porpoiseconfiguration);

请帮助:(

1 个答案:

答案 0 :(得分:2)

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

您无法写入http://位置,http包装器不支持它。

改为使用本地文件路径。