Visualizzazione dei risultati da 1 a 3 su 3

Discussione: DOM e XML

  1. #1

    DOM e XML

    Ho il file XML:
    codice:
    <?xml version="1.0"?>
    <chess>
    <board>
    <id>123</id>
    <develope>false</develope>
    <move></move>
    </board>
    </chess>
    Ho la seguente classe xml.php:
    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);
        } 
    //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

    ?>
    Ora eseguo il seguente codice:
    Codice PHP:
    <?php

    include("xml.php");
    include(
    "chess.php");
    include(
    "chessboard.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

        
    //if
        
    $xml->save();
        
    $xml->unlock();
        
    $xml->close();
    //if

    ?>
    Ma produce l'output:
    codice:
    object(DOMNodeList)#72 (0) { }
    Fatal error: Call to undefined method DOMNodeList::appendChild() on line 73 (file xml.php)
    Dove sta il problema?

  2. #2
    up

  3. #3
    up

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.