L'errore segnalato è nella funzione addMove(), ultima riga.
Fatal error: Call to undefined method DOMNodeList::appendChild().
Codice PHP:
<?php
class Xml {
public $file;
public $fp;
public $dom;
public $xml;
public function __construct ($file) {
$this->file=$file;
$this->fp=fopen($file,"r+");
$this->dom=new DOMDocument();
$this->xml="";
} //__construct
public function lock () {
if (flock($this->fp,LOCK_EX))
return true;
return false;
} //lock
public function unlock () {
flock($this->fp,LOCK_UN);
} //unlock
public function load () {
$this->xml=fread($this->fp,filesize($this->file));
$this->dom->loadXML($this->xml);
} //load
public function save () {
$this->xml=$this->dom->saveXML();
fseek($this->fp,0,SEEK_SET);
fwrite($this->fp,$this->xml);
ftruncate($this->fp,ftell($this->fp));
} //save
public function close () {
fclose($this->fp);
} //close
public function getValue ($node) {
return $node->nodeValue;
} //getValue
public function getUndevelopedBoard () {
$board=$this->dom->getElementsByTagName("board");
foreach ($board as $board)
if ($board->getElementsByTagName("develope")->item(0)->nodeValue=="false")
return $board->getElementsByTagName("id")->item(0);
return false;
} //getUndevelopedBoard
public function createNewBoard ($id) {
$root=$this->dom->documentElement;
$board=$this->dom->createElement("board");
$id=$this->dom->createElement("id",$id);
$board->appendChild($id);
$develope=$this->dom->createElement("develope","false");
$board->appendChild($develope);
$move=$this->dom->createElement("move","");
$board->appendChild($move);
$root->appendChild($board);
} //createNewBoard
public function addMove ($board, $idMove) {
$move=$this->dom->createElement("id",$idMove);
$moves=$board->getElementsByTagName("move");
//
var_dump($moves);
//
$moves->appendChild($move); //ERRORE
} //addMove
public function existBoard ($id) {
$board=$this->dom->getElementsByTagName("board");
foreach ($board as $board)
if ($board->getElementsByTagName("id")->item(0)->nodeValue==$id)
return true;
return false;
} //existBoard
} //Xml
?>
Codice PHP:
<?php
$xml=new Xml('chess.xml');
if ($xml->lock()) {
$xml->load();
$undevelopedBoard=$xml->getUndevelopedBoard();
$undevelopedBoardValue=$xml->getValue($undevelopedBoard);
if ($undevelopedBoard!=false) {
$chessboard=new Chessboard($undevelopedBoardValue);
$chessboard->mount();
for ($i=0;$i<8;$i++)
for ($j=0;$j<8;$j++)
if ($chessboard->chessboard[$i][$j]->getRank()!="EMPTYBOX")
if ($chessboard->chessboard[$i][$j]->color==$chessboard->turn)
for ($m=0;$m<8;$m++)
for ($n=0;$n<8;$n++) {
$chessboard->cloneBoard();
if ($chessboard->chessboard[$i][$j]->move($m,$n,$chessboard->chessboard,$chessboard->board))
if($chessboard->check($chessboard->turn)==false) {
$chessboard->unmount();
$xml->addMove($undevelopedBoard,$chessboard->output);
if ($xml->existBoard($chessboard->output)==false) {
$xml->createNewBoard($chessboard->output);
} //if
} //if
} //for
//develope
} //if
$xml->save();
$xml->unlock();
$xml->close();
} //if
?>
Codice:
<?xml version="1.0"?>
<chess>
<board>
<id>123</id>
<develope>false</develope>
<move></move>
</board>
</chess>