3초기억력

php에서 xml 데이타 파싱하기 - xmlparser function 본문

플밍_기타

php에서 xml 데이타 파싱하기 - xmlparser function

잠수콩 2010. 8. 31. 18:15
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.




PHP 에서 XML 데이타 파싱하는 클래스입니다.
몇가지 업그레이드 될 여지는 있는 함수이지만 일반적으로 사용하기엔 별무리없이 사용가능합니다.

<?
///////////////////////////////////////////
///--  XML 파싱 클래스 사용방법  --///
//////////////////////////////////////////

//XMLParser를 생성합니다.
$parser = new XmlParser('http://www.newjinbo.org/xe/rss');

//파싱된 데이타에서 필요한 노드를 뽑아옵니다.
$items = $parser->getData('RSS/CHANNEL/ITEM');

//뽑아온 노드를 루프를 돌리면서 최종노드의 값을 받아서 처리합니다.
foreach($items as $item){
    echo "<a href='".$item['LINK']."'>".$item['TITLE'],"</a></br>";
}
?>

 


<?php
//----- XML 파싱 클래스 ---//
class XmlParser {
    public $parser;
    public $depth=0;
    public $termStack;
    public $nodeData;
    public $fullParseData;
    public $prevdepth;
    public $uri;
    public $last_node;
    public $inside_data;

    function XmlParser($uri)
    {
        $this->setURI($uri);
        $this->run();
    }

    function run()
    {
        $this->termStack = array();
        $this->xmlInit();
        $this->parsing();
    }

    function setURI($uri)
    {
        $this->uri = $uri;
    }

    function xmlInit()
    {
        $this->parser = xml_parser_create();
        if(!$this->parser) echo "Parser Error<br>";
        if(!xml_set_object($this->parser, $this)) echo "xml set object error<br>";
        if(!xml_set_element_handler($this->parser, "tag_open", "tag_close")) echo "handler set error<br>";
        if(!xml_set_character_data_handler($this->parser, "cdata")) echo "cdata handler error<br>";
    }

    function cdata($parser, $cdata)
    {
        if($this->depth > $this->prevdepth)
        {
            if($this->inside_data)
                $this->nodeData[$this->nodeName()] .= $cdata;
            else
                $this->nodeData[$this->nodeName()] = $cdata;
            $this->last_node = $this->nodeName();
        }
        $this->inside_data=true;
    }

    function getData($node=null)
    {
        if($node == null)
        {
            return $this->fullParseData;
        }
        return $this->fullParseData[$node];
    }

    function parsing()
    {
        $fp = fopen($this->uri, "r");
        if(!$fp)
        {
            return 0;
        }
        while($data = fread($fp, 9182))
        {
            $this->parse($data);
        }
        fclose($fp);
        return 1;
    }

    function parse($data)
    {
        if(!xml_parse($this->parser, $data)) echo xml_error_string(xml_get_error_code($this->parser));
    }

    function getpNode($depth=0)
    {
        if($depth != 0)
        {
            $node=count($this->termStack) + $depth;
            $stack = array_slice($this->termStack, 0, $node);
        }
        else
        {
            $stack = $this->termStack;
        }
        return join("/",$stack);
    }

    function pushStack($name)
    {
        array_push($this->termStack, $name);
    }


    function getStackSize()
    {
        return count($this->termStack);
    }

    function tag_open($parser, $tag, $attributes)
    {
        $this->pushStack($tag);
        if($this->depth > $this->prevdepth)
        {
            if(count($this->nodeData))
            {
                $last_node = $this->getpNode(-2);
                $this->fullParseData[$last_node] = $this->nodeData;
            }
            $this->nodeData=array();
            $this->prevdepth = $this->depth;
        }
        $this->depth++;
        $this->inside_data=false;
    }

    function tag_close($parser, $tag)
    {
        $count = count($this->nodeData);
        if($count == 0)
            array_pop($this->termStack);
        $this->depth--;
        if($this->depth < $this->prevdepth)
        {
            if(count($this->nodeData) > 1)
                $this->fullParseData[$this->getpNode()][] = $this->nodeData;
            else
                $this->fullParseData[$this->getpNode()] = $this->nodeData;
            $this->nodeData=array();
        }
        else
        {
            $this->prevdepth = $this->depth;
        }
        if($count != 0)
            array_pop($this->termStack);
                $this->inside_data=false;
    }

    function nodeName()
    {
        return $this->termStack[$this->depth-1];
    }

}

?>


출처 : http://gogosim.tistory.com/78




 




'플밍_기타' 카테고리의 다른 글

SQL injection Tools  (0) 2010.09.08
ietester download url  (0) 2010.09.02
윈도우 탐색기 최초 경로 지정법  (0) 2010.08.25
sql 2005 express edition download URL  (0) 2010.07.23
sitegalaxyupload 설치버전, DLL 버전  (0) 2010.07.02
Comments