블로그   |    태그로그   |    방명록
민준이
전체 (120)
공부 (49)
포트폴리오 (70)
공지사항 (0)
자유게시판 (1)
«   2008/11   »
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            
월별보기
'2008/11'에 해당되는 글 3건
rss 내용을 파싱하는 간단한 함수입니다.
Category : 공부/PHP Date : 2008/11/21 10:33
2008/11/21 10:33 2008/11/21 10:33
rss 내용을 파싱하는 간단한 함수입니다.
(rss 파일은 fopen이나 fsocket 등등 알아서 읽어오셔야합니다;;)
HTML 파싱용으로도 사용가능합니다 덜덜;;

function _parser($tag, $str){
    preg_match_all("/<".$tag.">(.*)<\/".$tag.">/iUs", $str, $match);
    for($i=0, $total=sizeof($match[1]); $i<$total; $i++){
        $match[1][$i]=str_replace("<![CDATA[", "", $match[1][$i]);
        $match[1][$i]=str_replace("]]>", "", $match[1][$i]);
    }
    return $match[1];
}

원리는 간단합니다.
<a>
  <b>하하하</b>
  <b>
    <c>aaa하하하</c>
    <c>222</c>
  </b>
</a>

이런구조를 파싱하시려고 할때 $tmp 변수에 위 내용이 있다고 가정할때

* a 태그를 가져올때
$a = _parser("a", $tmp);

$a[0]의 값
<b>하하하</b>
  <b>
    <c>aaa하하하</c>
    <c>222</c>
  </b>


* b 태그를 가져올때
$b = _parser("b", $a[0]);

$b[0]의 값
하하하

$b[1]의 값
<c>aaa하하하</c>
<c>222</c>


* c 태그를 가져올때
$c = _parser("c", $b[1]); // 두번째 b 태그에만 c태그가 들어있으므로

$c[0]의 값
aaa하하하

$c[1]의 값
222

----------------------------------------------------------------------------------------------


*  UTF-8로 인코딩 처리하기
 $data 변수에 rss 내용이 있을경우
    if(preg_match("/encoding=\"(.*)\"/Us", $data, $match)){
        if(strtolower($match[1]) != "utf-8"){
            $data=iconv($match[1], "UTF-8", $data);
        }
    }

-------------------------------------------------------------------------------------------------

* 위 정보를 토대로 RSS 파싱하기
$data 변수에 rss 정보가 들어있다고 가정합니다.
        list($channel) = _parser("channel", $data);

        // RSS 제공하는 제목입니다.
    list($channel_title) = _parser("title", $channel);

        // RSS url 입니다.
    list($channel_link) = _parser("link", $channel);

        // RSS 설명입니다.
    list($channel_description) = _parser("description", $channel);

      echo '<h2><a href="'.$channel_link.'" target="_blank">'.$channel_title.'</a> : '.$channel_description.'</h2>'.$list.'</ul>';

    $channel_item = _parser("item", $channel);        
    for($i=0, $total=sizeof($channel_item);$i<$total; $i++){
        $item = $channel_item[$i];
        list($title) = _parser("title", $item); // RSS기사 제목
        list($link) = _parser("link", $item); // RSS기사 url
        list($pubDate) = _parser("pubDate", $item); // RSS기사 날짜
        $pubDate= date("m/d", strtotime($pubDate));

        echo '<li>'.$date.' <a href="'.$link.'">'.$title.'</a></li>';
    }

상위로
엮인글 쓰기 0 | 덧글 쓰기 0 | 목록 열기
[Prototype] 자바스크립트를 이용한 XML 파싱 클래스
Category : 공부/XML Date : 2008/11/21 10:32
2008/11/21 10:32 2008/11/21 10:32
[Prototype] 자바스크립트를 이용한 XML 파싱 클래스

*****************************************************

이번에 소개하는것은 Prototype 프레임워크상에서의 예제입니다.
http://prototype.conio.net

rss는 태터툴즈를 기본으로 설명하였습니다.

주    의 : 제가 설명을 잘 못합니다 :)

*****************************************************

예전에 제가 PHP로 xml 또는 html 를 파싱하는 간단한 소스를 소개한적이 있죠
http://www.phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=49208&sca=&sfl=wr_name%7C%7Csubject&stx=%BD%BA%B3%AA&sop=and

이번에는 위와 비슷한 방식으로 자바스크립트로 XML을 파싱합니다.^^

일단 파싱 클래스 소스입니다.

클래스는 두개입니다. (* 프로토타입을 기반으로 하여 만들었습니다.)

* xml 클래스
노드 파싱시 필요한 함수가 있는 부모 클래스

* xml 클래스를 확장한 rss 클래스 ( extend xmlParser )
실질적으로 필요한 노드를 파싱하는 자식 클래스

--------------------------------------------------------------------------------------------------

// XML 클래스
var xmlParser = Class.create();
Object.extend(xmlParser.prototype, {
    initialize: null,
    getNode: function (xml, tag){ // 싱글노드
        return xml.getElementsByTagName(tag)[0];
    },
    getNodeAll: function (xml, tag){ // 멀티노드
        return xml.getElementsByTagName(tag);
    },
    getValue: function (xml){ // 노드값 가져오기
        try{
            return xml.firstChild.nodeValue;
        } catch(e){
            return null;
        }
    }
});

// XML 클래스를 확장한 RSS 클래스
var rssParser = Class.create();
Object.extend(rssParser.prototype, xmlParser.prototype);
Object.extend(rssParser.prototype, {
    title: null,
    link: null,
    description: null,
    language: null,
    pubDate: null,
    generator: null,
    image: null,
    item: null,

    initialize: function (xml){        
        // CHANNEL
        var channel = this.getNode(xml, "channel");
        //-----------------------------------------------------------------
        // + TITLE
        this.title = this.getValue(this.getNode(channel, "title"));
        // + LINK
        this.link = this.getValue(this.getNode(channel, "link"));
        // + DESCRIPTION
        this.description = this.getValue(this.getNode(channel, "description"));
        // + LANGUAGE
        this.language = this.getValue(this.getNode(channel, "language"));
        // + PUBDATE
        this.pubDate = this.getValue(this.getNode(channel, "pubDate"));
        // + GENERATOR
        this.generator = this.getValue(this.getNode(channel, "generator"));
        // + IMAGE
        var img_node = this.getNode(channel, "image");
        var img_obj = new Object();
        img_obj.title = this.getValue(this.getNode(img_node, "title"));
        img_obj.url = this.getValue(this.getNode(img_node, "url"));
        img_obj.link = this.getValue(this.getNode(img_node, "link"));
        img_obj.width = this.getValue(this.getNode(img_node, "width"));
        img_obj.height = this.getValue(this.getNode(img_node, "height"));
        img_obj.description = this.getValue(this.getNode(img_node, "description"));
        this.image = img_obj;
        //-----------------------------------------------------------------
        // + ITEM
        var item_node = this.getNodeAll(channel, "item");
        var item_list = new Array();
        for(var i=0; i<item_node.length; i++){        
            var item_obj = new Object();
            item_obj.title = this.getValue(this.getNode(item_node[i], "title"));
            item_obj.link = this.getValue(this.getNode(item_node[i], "link"));
            item_obj.description = this.getValue(this.getNode(item_node[i], "description"));

            var category_node = this.getNodeAll(item_node[i], "category");
            var category_list = new Array();
            for(var j=0; j<category_node.length; j++){
                category_list.push(this.getValue(category_node[j]));
            }
            item_obj.category = category_list;

            item_obj.author = this.getValue(this.getNode(item_node[i], "author"));
            item_obj.guid = this.getValue(this.getNode(item_node[i], "guid"));
            item_obj.comments = this.getValue(this.getNode(item_node[i], "comments"));
            item_obj.pubDate = this.getValue(this.getNode(item_node[i], "pubDate"));

            item_list.push(item_obj);
        }
        this.item = item_list;
    }
});


--------------------------------------------------------------------------------------------------

* 사용법

new Ajax.Request(
    -- 생략 --
    onSuccess: function(request){
        var xml = new rssParser(request.responseXML);    

        var h = document.createElement("h3");
        h.innerHTML = xml.title;
        $('debug').appendChild(h);

        for(var i=0; i<xml.item.length; i++){    
            var li = document.createElement("li");
            var a = document.createElement("a");
            a.href = xml.item[i].link;
            a.innerHTML = xml.item[i].title;
            li.appendChild(a);

            $('debug').appendChild(li);
        }
    }
    -- 생략 --
});    


* rssParser의 가져오는 노드값 간단 정리~!!
<태그>가져오는방식(xml.태그)</태그>
var xml = new rssParser(request.responseXML);    

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
    <channel>
    <title> xml.title </title>
    <link> xml.link </link>
    <description> xml.description </description>
    <language> xml.language </language>
    <pubDate> xml.pubDate </pubDate>
    <generator> xml.generator </generator>
    <image>
        <title> xml.image.title </title>
        <url> xml.image.url </url>
        <link> xml.image.link </link>
        <width> xml.image.width </width>
        <height> xml.image.height </height>
        <description> xml.image.description </description>
    </image>
    <item>
        <title> item[0].title </title>
        <link> item[0].link </link>
        <description> item[0].description </description>
        <category> item[0].category[0] </category>
        <category> item[0].category[1] </category>
        <category> item[0].category[2] </category>
        -- category 반복 생략 --
        <author> item[0].author </author>
        <guid> item[0].guid </guid>
        <comments> item[0].comments </comments>
        <pubDate> item[0].pubDate </pubDate>
    </item>
    <item>
        <title> item[1].title </title>
        <link> item[1].link </link>
        <description> item[1].description </description>
        <category> item[1].category[0] </category>
        -- category 반복 생략 --
        <author> item[1].author </author>
        <guid> item[1].guid </guid>
        <comments> item[1].comments </comments>
        <pubDate> item[1].pubDate </pubDate>
    </item>
    -- item 반복 생략 --
    </channel>
</rss>

상위로
엮인글 쓰기 0 | 덧글 쓰기 0 | 목록 열기
오라클 함수 모음집
Category : 공부/ORACLE Date : 2008/11/10 18:35
2008/11/10 18:35 2008/11/10 18:35
http://kr.php.net/manual/kr/function.ocibindbyname.php

상위로
엮인글 쓰기 0 | 덧글 쓰기 0 | 목록 열기
Creative Commons License 블로그 내에 모든 저작물은 크리에이티브 커먼즈코리아 저작자표시 - 비영리 - 변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
최근 올라온 글
블로그 최근 덧글
최근 엮인글
Phentermine 37.5 mg online...
 Cheapest phentermine online.
Buy cheap tramadol mg table...
 Cheap tramadol prescription...
How much tickets are sold f...
 Air flight tickets.
Valtrex.
 Valtrex medication.
Buy cheap phentermine.
 Cheap phentermine cod.
즐겨찾기
Today : 19
Yesterday : 50
Total : 13679
Powered by Textcube 1.7.1 : Risoluto
Skin by mulder21c
RSS 주소보기 E-Mail 보내기