PHP | xml로 저장하기 | |||||
---|---|---|---|---|---|
작성자 | 작성일 | 18-03-07 18:50 | |||
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> |
|||||
|
댓글목록
등록된 댓글이 없습니다.