728x90

XML 데이터 생성하여 화면에 보여주거나, 파일로 저장하는 방법에 대한 함수를 시간을 많이 할애하고 테스트하고 적어둔다.

작업을 하다보니 XML 데이터의 칼럼수가 달라지는 경우 일일이 적어주는 것도 일이더라.

그래서 함수로 만들어서 사용하니 매우 편하다.

dbClass.php 에 대한 설명은 http://link2me.tistory.com/1110 참조하면 된다.


<?php
    include_once $_SERVER['DOCUMENT_ROOT'].'/db.info.php';
    require_once $_SERVER['DOCUMENT_ROOT'].'/phpclass/dbClass.php';
    require_once $_SERVER['DOCUMENT_ROOT'].'/phpclass/xmlClass.php';

    $conn = new MySQLDbClass(); // DB 함수
    $DB_CONNECT = $conn->isConnectDb($DB);

    // 화면에 출력할 칼럼 발췌
   $sql ="select (select classname from category where uid=d.cate1) as cat1,(select classname from category where uid=d.cate2) as cat2,subject,content from data d where is_direct=2 and is_checking=0";
   $result = mysql_query($sql,$DB_CONNECT);

    $c = new xmlcreateClass();
    $c->XML2View($result); // XML 데이터 화면 출력
    //$c->XML2File($result); // XML 데이터 파일 서버에 저장
    //$c->XMLDownload($result, 'myTest'); // XML 데이터 파일 PC에 저장

?>



<?php
class xmlcreateClass {

    // MySQL DB 자료를 column 개수, 게시물 총개수 만큼 자동으로 XML로 만들기
    function exportAsXML($result){
        $xml ='<?xml version="1.0" encoding="UTF-8" ?>'."\n";
        $xml.='<ListInfo>'."\n";
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $xml.="<items>"."\n";
            foreach($row as $column => $value) {
                $xml.="<".$column."><![CDATA[".$value."]]></".$column.">"."\n";
            }
            $xml.="</items>"."\n";
        }
        $xml.='</ListInfo>';
        return $xml;
    }

    // XML 데이터를 모니터 화면으로 출력
    // $c->XML2View($result); // XML 데이터 화면 출력
    function XML2View($result) {
        header('Content-type: text/xml');
        echo $this->exportAsXML($result);
    }

    // XML 데이터를 파일로 서버 저장
    function XML2File($result) {
        $destination_path='files/';
        $xmlfile ='XMLSaveFile.xml';
        $filename = $destination_path . $xmlfile;

        @chmod($filename,0707);  // 파일의 소유권이 있는 경우에만 권한 변경

        $file = fopen($filename,"w");
        fwrite($file, $this->exportAsXML($result));
        fclose($file);

        echo '<script>alert("파일저장 완료");</script>';
    }

    // XML 생성 데이터를 PC에 파일로 저장
    function XMLDownload($result, $filename) {
        header("Content-type: application/vnd.ms-excel; charset=UTF-8");
        header("Content-Disposition: attachment; filename = ".$filename."_".date("Ymd").".xml");
        header("Content-Description: PHP5 Generated Data");
        header("Cache-Control: no-cache"); header("Pragma: no-cache");
        echo $this->exportAsXML($result);
        exit;
    }

}  // end of Class
?>

블로그 이미지

Link2Me

,