PHP | xml로 저장하기
작성자
DB의 내용을 PHP로 파싱을 해서 xml파일로 저장시키는 방법이 있습니다.
예제로 DB내의 내용이 아래와 같다면

+----------+------+------------+
| searchW  | cnt  | date       |
+----------+------+------------+
| 싸이월드 | 1784 | 2006-11-27 |
| 야후     |  709 | 2006-11-27 |
| 옥션     |  643 | 2006-11-27 |
+----------+------+------------+

파싱시키는 예제소스는 아래와 같습니다.

<?
##DB연결
$sql = "select * from table"; //DB에서 정보가져오기
$rs = mysql_query($sql);
while($result = mysql_fetch_array($rs)){
     $searchW = $result[searchW];
     $cnt = $result[cnt];
     $date = $result[date];
     
//DB에서 가져온 정보로 Xml문서의 Element를 구성  $xmlList.="<word><searchW>$searchW</searchW><cnt>$cnt</cnt><date>$date</date></word>";
}
mysql_free_result($rs);

//통합 XmlDoc의 내용을 구성
$xmlDoc="<?xml version='1.0' encoding='euc-kr'?> <root>$xmlList</root>";

//XmlDoc의 내용을 가지고 xml파일 생성하기
$fp = fopen("test.xml","w");
fput($fp, $xmlDoc);
fclose($fp);

##DB연결해제
?>
그렇다면 만들어진 결과물 test.xml파일은 아래와 같은 내용의 xml파일이 될겁니다.
test.xml::
<root>
     <word>
          <searchW>싸이월드</searchW>
          <cnt>1784</cnt>
          <date>2006-11-27</date>
     </word>
     <word>
         <searchW>야후</searchW>
         <cnt>709</cnt>
         <date>2006-11-27</date>
     </word>
     <word>
          <searchW>옥션</searchW>
          <cnt>643</cnt>
          <date>2006-11-27</date>
     </word>
</root>

댓글목록

등록된 댓글이 없습니다.

총 게시물 93 개, 페이지 6 RSS
제목 작성자
PHP txt파일 읽어오기 H
PHP xml로 출력하기 H
PHP xml로 저장하기 H
PHP utf-8 문자열 자르기 함수 mb_strimwidth H
PHP rss리더기 H
PHP PHP 문서를 MS-Office 파일 문서로 변환 H
PHP parse_url함수 사용으로 url찾기 H
PHP ImageCopyResized,imagecopyresampled - 비슷한기능&&나은퀄리티 H
PHP html문서로 저장 H
PHP 입력값의 HTML 태그 없애기 H
PHP 문자셋 변환(euc-kr ->utf-8 ) H
PHP 파일처리함수 H
PHP 문자열처리함수 H
PHP 난수 생성(랜던생성) H
PHP 디렉토리 폴더만보기 H
게시물 검색