<?
// ±¾È˲»ÊiϤXML£¬¿ÉÄÜ×¢½a¶ÔXMLµÄÃeÊoÓÐÎo
// ÕaÖ»ÊǸo˼Ïe£¬ÒªÊµÏֵĻ°Ó¦¸ÃÂÔ¼ÓÐÞÕu£¬¿ÉÒÔ°ÑÒ»Ð(C)²»³£ÓÃÌo¼þ²eѯµÄÊý¾ÝÓÃXML±£´ae
// ÈçÐeÄaÉçÇøµÄ Óû§×ÊÁÏ£¬Ò»°aÖ»ÓÐÓû§×Ô¼º»oÆaËuÓû§Ö¸¶¨ÁËËuµÄUSERNAME²Å»aÏÔʾ
// Ê£ÏÂÿ´Î¶¼Òª¿ªÆo¡¢²eѯ¡¢½aÊÍÊý¾Ý¼¯¡¢¹Ø±ÕÊý¾Ý¿aµÄÏuºÄ¡£
define("enter",chr(13).chr(10));
class DATA_XML //¹Ø¼u£¡£¡£¡Ò»¸o×Ô¶¨ÒaÀa
{
var $parser; //XML½aÊÍÆ÷
var $tags; //XML±eÇ(C)
var $on; //
var $root; // ¸uÔªËØ
var $Data = array(); // ÔªËØÊý×e
function DATA_XML($filename,$root) //Àa³oʼº¯Êý filenameÎļþÃu,root¸uÔªËØ
{
$this->root = $root; // ³oʼ¸uÔªËØ
$this->parser = xml_parser_create();//´´½¨½aÊͶÔÏo
xml_set_object(&$this->parser,&$this);//ÉeÖöÔÏo
xml_set_element_handler($this->parser,"tag_on","tag_off");//ÉeÖÃÔªËعÜÀiº¯Êý
xml_set_character_data_handler($this->parser,"getdata"); //ÉeÖÃÊý¾Ý¹ÜÀiʼþ
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); //ÒªÇoÑϸñÇø*Ö´oСд
if( file_exists($filename) ) //ÕÒµ½ÎļþÂð£¿
{//ÕÒµ½
$fp = fopen($filename,"r"); // Ö»¶Á´o¿ª
$c = fread($fp,filesize($filename)); //¶ÁÈ¡Êý¾Ý
fclose($fp);//¹Ø±Õ
$this->parse($c); //ÉeÖýaÊͶÔÏo
}
else
{
echo "ooooo"; //£¡£¿£¡ºÃÏoÊǵ÷ÊÔʱÓõģ¬È¥µo elseºÃÁË
}
}
function parse($data)//½aÊͺ¯Êý
{
xml_parse($this->parser,$data);
}
function tag_on($parser,$tag,$attributes)//¢ÏÖÔªËØ´¥¢º¯Êý
{
$this->on = true; // ÒѾ´¥*¢¹ý
$this->tags = $tag;
//echo $tag;
}
function tag_off($parser,$tag) //ÔªËؽaËغ¯Êý
{
$this->on = false; // ÔªËؽaËØ
}
function getdata($parser,$cdata) //È¡³oÔªËØ
{
if($this->on && $this->tags!=$this->root)
{//Ñe֤һϱȽϺ㬣¨ÏÖÔÚÊÇ*ñΪԪËصĿªÊ¼£¬²¢ÇÒ²»ÊǸuÔªËØ£¿£(C)
$this->Data[$this->tags]=trim($cdata);
// ¹Ø¼u°¡£¡£¡ÓñeÇ(C)×oÊý×e±i£¬²¢¸øÕa¸oÊý×eÔªËظ¶ÉÏÊý¾Ý
//echo trim($cdata);
//echo $this->tags;
//echo "\n";
}
}
function check($str)
{//¼i²e×Öu
if( strlen($str)<1 )//Èç¹uÊÇ¿Õ×Öu
return ' ';//µ»Ø ' '
else
return $str;//ñÔoÔÂ**µ»Ø£¨²»È»XMLºÃÏo»a²»×o½aÊø±e¼Ç£(C)
}
function saveas($filename)//Ái´aeΪ
{
$c='<?xml version="1.0" encoding="gb2312"?>'.enter;
//Õa¸o²»ÓÃ˵ÁË°É£¬enterÊÇdefine("enter",chr(13).chr(10));
$c.="<".$this->root.">".enter;
//Ê×ÏÈÉe¶¨Ò»Ï¸uÔªËØ
for( reset($this->Data);$i=key($this->Data);next($this->Data))
{//¿ªÊ¼¾Ù³oËuÓÐÊý¾ÝÀ²
$c.= "<".$i.">".$this->check($this->Data[$i])."</".$i.">".enter;
// ÓÃÊý×e±iÃu×oÔªËرeÇ(C)£¬²¢¼ÓÉÏÊý¾ÝÄÚÈÝ
}
//echo $c;
$c.="</".$this->root.">".enter;//½aÊø±eÇ(C)
$fp = fopen( $filename , "w" );//дÈeÎļþ
fwrite($fp,$c);
fclose($fp);
}
};
/мuÒ»¸oXMLÎļþ
$xml = new DATA_XML(¾¶."arm.xml","DATA"/¸uÔªËØ/);
$xml->Data["UserName"]="¹ØÓð";
$xml->Data["Nick"]="¹ØÔƳ¤";
$xml->saveas( ¾¶."arm.xml");
unset( $xml );
// ¶ÁÈ¡Ò»¸oÎļþ
$xml = new DATA_XML(¾¶."arm.xml","DATA");
echo $xml->Data["Nick"];//ÏÔʾһÏ£¿
$xml->Data["Nick"]="¹ØÀÏÒ¯";//UPDATE
$xml->Data["Master"]="Áo±¸";//INSERT INTO
$xml->saveas(¾¶."arm.xml");//Ái´aeΪ¸²¸Ç±¾Éi
/
?>