XML 로드 클래스

XML 클래스 입니다..
아니 이게 왜 필요하냐?

그딴 이유 없음;

php5에서만 동작합니다.
사용 설명은 주석에~

최소한에 new 연산자 아는분만 쓰셈.~
<?
class xmlLoad {
/*
* 작성자 : 다프린(불바람)
* 공짜인데 이름 지우면 죽여불라~
*
*
* 사용법:
* 1번 방법
* $this->{'data'} = "<data><title></title></data>";
* 위에 처럼하면 data로 호출시 위에 데이터가 들은 DOM객체를 셋팅되고 data로 부르면 된다.
*
* 2번 방법
* $this->{'http://babo.net/나XML.xml'};
* 위에 처럼하면 http://babo.net/wow.xml로 호출시 해당 xml의 데이터가 들은 DOM객체를 반환
* 3번방법
* $this->{'wow2.xml'} = "<data><title></title></data>";
* XML 확장자를 가진 이름이면 파일이 없으면 해당 자료를을 넣은 데이터 DOM이 세팅된고 마지막 작업때 자공 저장된다.
* 해당 파일이 있으면 wow2.xml를 불러온다.
*
* !!!!이미 있는 파일 변조하고 다시 저장시.
* $this->{'나XML.xml'} = $this->{'나XML.xml'}->firstChild->ownerDocument;
*
* 이렇게 다시 Document를 넣으면 종료 될때 자동 저장된다.
* 주의:: 같은 DOM객체여야 한다. 서로 틀린 DOM 경우 전혀 안된다. 프로토콜로 시작하는 이름은 에러가 걸리니 주의 해라.
*
* 이하 메소드 설명 귀찮아서 안함.
*/

private $version='1.0', $encoding='utf-8';
private static $node = array(), $stand_by_save = array();

private $extension = array('xml', 'xsl');

public function __construct(){
}

final private function findType($key){
if(strpos($key, "http://") === 0 or strpos($key, "https://") === 0 or strpos($key, "ftp://") === 0) return 'url';
else{
$ex = end(explode(".", strtolower($key)));
return array_search($ex, $this->extension) !== false?'localhost':'normal';
}
}

final public function __get($key){
if($this->isKey($key)) return $this->loadKey($key);
else{
$type = $this->findType($key);
switch ($type) {
case 'localhost':
if(!file_exists($key)) return false;
case 'url':
$dom = $this->load($key, $this->version, $this->encoding, $type);
$this->saveKey($key, $dom);
return $dom;
break;
}
}
return false;
}

final public function __set($key, $val){
if(!$this->isKey($key)){
if(get_class($var) == 'DOMDocument') {
$dom = $this->loadKey($key);
if($dom->check_code == $val->check_code) $this->stand_by_save($key);
return $dom;
}
else{
$type = $this->findType($key);
switch ($type) {
case 'normal':
$dom = $this->load($val, $this->version, $this->encoding, $type);
$this->saveKey($key, $dom);
return $dom;
break;
case 'localhost':
if(!file_exists($key)){
$dom = $this->load($val, $this->version, $this->encoding, $type);
$this->saveKey($key, $dom);
$this->stand_by_save($key);
return $dom;
}
break;
}
}
}
}

public function __isset($name) {
return isset(self::$node[$name]);
}

public function __unset($name) {
unset(self::$node[$name]);
}

final private function saveKey($key, $dom){
if(!self::$node[$key]){
$dom->check_code = count(self::$node);
self::$node[$key] = $dom;
return true;
}
return false;
}

final private function loadKey($key){
if(self::$node[$key]) return self::$node[$key];
return false;
}

final private function isKey($key){
if(self::$node[$key]) return true;
return false;
}

final protected function load($val='', $version='1.0', $encoding='utf-8', $type=''){
if(_debug_) $xmlLoadTime = microtime_float();
$dom = new DOMDocument($version, $encoding);
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
if($type == '') $type = $this->findType($val);
if($val != ''){
switch ($type) {
case 'normal':
$dom->loadxml("<?xml version=\"".$version."\" encoding=\"".$encoding."\" ?>".$val);
break;

case 'url':
case 'localhost':
$dom->load($val);
break;
}
}
$dom->type = $type;
if(_debug_) $GLOBALS["xmlLoadTime"] += bcsub(microtime_float(), $xmlLoadTime, 4);
return $dom;
}

final protected function save(DOMDocument $dom, $fileName){
switch ($dom->type) {
case 'localhost':
case 'normal':
$dom->save($fileName);
break;
}
}

final public function stand_by_save($key, $fileName=''){
if($this->isKey($key)){
switch ($this->loadKey($key)->type) {
case 'localhost':
self::$stand_by_save[$key] = $key;
return true;
break;
case 'normal':
self::$stand_by_save[$key] = $fileName;
return true;
break;
}
}
return false;
}

final public function __destruct(){
foreach(self::$stand_by_save as $key => $val) $this->save($this->loadKey($key), $val);
}
}<div class='small'>[이 게시물은 관리자님에 의해 2011-10-31 17:16:08 PHP & HTML에서 이동 됨]</div>
|

댓글 1개

써보고 답글 다시 적을께요.~
좋은 팁 감사합니다
댓글을 작성하시려면 로그인이 필요합니다. 로그인

프로그램

+
제목 글쓴이 날짜 조회
14년 전 조회 948
14년 전 조회 1,018
14년 전 조회 1,100
14년 전 조회 2,842
14년 전 조회 1,030
14년 전 조회 1,694
14년 전 조회 1,314
14년 전 조회 940
14년 전 조회 2,208
14년 전 조회 2,651
14년 전 조회 758
14년 전 조회 1,003
14년 전 조회 1,890
14년 전 조회 2,271
14년 전 조회 1,330
14년 전 조회 1,462
14년 전 조회 796
14년 전 조회 1,120
14년 전 조회 1,123
14년 전 조회 1,883
14년 전 조회 1,150
14년 전 조회 821
14년 전 조회 915
14년 전 조회 224
14년 전 조회 961
14년 전 조회 878
14년 전 조회 889
14년 전 조회 1,430
14년 전 조회 2,312
14년 전 조회 1,367
14년 전 조회 1,144
14년 전 조회 1,521
14년 전 조회 770
14년 전 조회 775
14년 전 조회 1,035
14년 전 조회 1,259
14년 전 조회 891
14년 전 조회 1,181
14년 전 조회 1,804
14년 전 조회 787
14년 전 조회 1,117
14년 전 조회 1,133
14년 전 조회 849
14년 전 조회 862
14년 전 조회 1,243
14년 전 조회 1,728
14년 전 조회 1,632
14년 전 조회 1,535
14년 전 조회 913
14년 전 조회 1,105
14년 전 조회 943
14년 전 조회 831
14년 전 조회 2,022
14년 전 조회 1,296
14년 전 조회 1,235
14년 전 조회 1,291
14년 전 조회 1,506
14년 전 조회 874
14년 전 조회 1,658
14년 전 조회 3,952
14년 전 조회 1,886
14년 전 조회 1,989
14년 전 조회 1,407
14년 전 조회 1,874
14년 전 조회 1,512
14년 전 조회 1,337
14년 전 조회 1,006
14년 전 조회 963
14년 전 조회 1,171
14년 전 조회 1,418
14년 전 조회 5,946
14년 전 조회 1,480
14년 전 조회 1,197
14년 전 조회 1,247
14년 전 조회 819
14년 전 조회 1,011
14년 전 조회 1,622
14년 전 조회 1,089
14년 전 조회 747
14년 전 조회 1,287
14년 전 조회 1,352
14년 전 조회 1,674
14년 전 조회 988
14년 전 조회 4,569
14년 전 조회 1,156
14년 전 조회 1,131
14년 전 조회 1,154
14년 전 조회 1,347
14년 전 조회 1,157
14년 전 조회 895
14년 전 조회 968
14년 전 조회 1,406
14년 전 조회 710
14년 전 조회 1,286
14년 전 조회 1,279
14년 전 조회 1,366
14년 전 조회 4,103
14년 전 조회 1,341
14년 전 조회 1,047
14년 전 조회 1,470
🐛 버그신고