Pages

Tuesday, January 11, 2011

Writing XML Document

/**
 * This class provide framework on how to write to xml file.
 * <?xml version="1.0"?>
 * <data>
 *    <entry>
 *        <name>...</name>
 *        <city>...</city>
 *    </entry>
 * </data>
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : january 11, 2010
 * @version : 1.0
 */


class XMLWrite{
   
    /** 
     * constructor service
     * @param filename of the xml document
     */
    public function __construct($file){
        $this->file = $file;
        $this->contents = file_get_contents($this->file);

    }

    /**
     * a method to insert the data to the xml document
     * @param the first text element
     * @param the second text element
     */
    public function insert($name,$city){
        $this->contents = str_replace("</data>","",
                                      $this->contents);
        $new_contents = $this->contents
                        ."<entry><name>".$name."</name>"
                        ."<city>".$city."</city></entry></data>";
        file_put_contents($this->file,$new_contents);
    }
}

No comments:

Post a Comment