YesYo.com MintState Forums
뒤로    YesYo.com MintState BBS > Tech > Ajax & Issue
검색
멤버이름    오토
비밀번호 
 

간단한 XML파싱 클래스 (PHP)

페이지 정보

작성자 MintState 댓글 0건 조회 19,234회 작성일 08-10-28 13:17

본문

간단한 XML파싱 클래스 (PHP)

<?php 
/*------------------------------------------------------------------* 
* 개 발 자 : 윤석규                                                     
*-------------------------------------------------------------------* 
* 이 메 일 : nanummp3@naver.com                                         
*-------------------------------------------------------------------* 
* 홈페이지 : http://www.ysksoft.com                                     
*-------------------------------------------------------------------*/

class yskXmlClass { 

    ## private 
    var $_xml_parser; 
    var $_xml_encoding; 
    var $_xml_chk        = 'n'; 
    var $_xml_item        = array(); 
    var $_xml_result    = array(); 

    /* 
    ## [실행 1] xml 열기 
    ------------------------------------------------------------------------------------------- 
    include './class/yskXmlClass.php'; 

    $xml = new yskXmlClass; 
    $prt = $xml->xmlOpen('http://blog.rss.naver.com/thinkfactory.xml','item'); 

    $count = count($prt['item']); 
    for($x=0; $x<$count; $x++) { 
        echo $prt['title'][$x]['value'].'<br>'; 
        echo $prt['link'][$x]['value'].'<br>'; 
        echo $prt['description'][$x]['value'].'<br><br><hr>'; 
    } 
    ------------------------------------------------------------------------------------------- 
    */ 
    function xmlOpen($url, $tag) { 
        $this->_tag = $tag; 
        if($fp = fopen($url, 'r')) { 
            while(!feof ($fp)) { 
                $xml_data .= fgets($fp, 4096); 
            } 
            fclose ($fp); 
            $this->_xmlDefine($xml_data); 
            return $this->_xmlInte(); 
        } else { 
            $this->_error('xml open error : xml 파일열기 실패 => '.$url); 
        } 
    } 

    ## xml 선언 
    function _xmlDefine($xml_data) { 
        preg_match('/encoding="[^"]+"/', $xml_data, $pattern); 
        $this->_xml_encoding = strtolower(preg_replace('/(encoding=)|(")/', '', $pattern[0])); 

        $this->_xml_parser = xml_parser_create(); 
        xml_parser_set_option($this->_xml_parser, XML_OPTION_CASE_FOLDING, 0); //태그 이름을 소문자로 뿌려줌 
        xml_parse_into_struct($this->_xml_parser, $xml_data, $this->_xml_item, $index); 
        xml_parser_free($this->_xml_parser); 
    } 

    ## xml 추출 
    function _xmlInte() { 
        foreach($this->_xml_item as $v) { 
            if($v['tag'] == $this->_tag && $v['type'] == 'open') { 
                $this->_xml_result[$v['tag']][] = ''; 
                $this->_xml_chk = 'y'; 
            } 
            if($v['type'] == 'complete' && $this->_xml_chk == 'y') { 
                if($this->_xml_encoding == 'utf-8') { 
                    $this->_xml_result[$v['tag']][] = array('value'=>iconv('utf-8', 'euc-kr', $v['value']),'att'=>iconv('utf-8', 'euc-kr', $v['attributes'])); 
                } else { 
                    $this->_xml_result[$v['tag']][] = array('value'=>$v['value'],'att'=>$v['attributes']); 
                } 
            } 
        } 
        return $this->_xml_result; 
    } 

    ## 에러표시 
    function _error($msg='') { 
        echo $msg; 
        exit; 
    } 
} 
?>

댓글목록

등록된 댓글이 없습니다.

Total 32건 2 페이지
Ajax & Issue 목록
번호 제목 글쓴이 조회 날짜
12 MintState 14725 02-19
11 MintState 15840 02-16
10 MintState 17360 02-11
9 MintState 12936 01-05
8 MintState 19322 11-17
열람중 MintState 19235 10-28
6 MintState 11888 10-28
5 MintState 15225 10-28
4 MintState 14713 10-28
3 MintState 10245 10-28
2 MintState 14615 10-28
1 MintState 11914 10-28
게시물 검색
모바일 버전으로 보기
CopyRight ©2004 - 2024, YesYo.com MintState. ™