CI 코드

제목 [팁] xml php4, php5 버젼대 사용예
글쓴이 ci세상 작성시각 2009/09/02 10:12:14
댓글 : 5 추천 : 0 스크랩 : 0 조회수 : 31913   RSS

php5에서는 dom 방식으로 쉽게 사용할 수 있게 내장되어 있는데 php4에는 없기때문에 좀 불편하긴 합니다.
그래서 php5대로 갈아타시길 권장해드립니다.^^


php 4버젼대

<?php
include "xml.php";

//Get the XML document loaded into a variable
$xml = file_get_contents('example.xml');
//Set up the parser object
$parser = new XMLParser($xml);

//Work the magic...
$parser->Parse();

foreach($parser->document->movie[0]->rating as $rating)
{    
    //If the rating is in stars...
    if($rating->tagAttrs['type'] == 'stars')
        echo $rating->tagData.' stars';

    //If the rating is in thumbs...
    if($rating->tagAttrs['type'] == 'thumbs')
        echo $rating->tagData.' thumbs up';
}


?>
=> 첨부1(php4.zip) 소스 참조


php5 버젼대

### 컨트롤러 부분 ###
<?php

class Welcome extends Controller {

	function Welcome()
	{
		parent::Controller();	
	}
	
	function index()
	{

		include 'example.php';
		$xml = new SimpleXMLElement($xmlstr);
		echo $xml->movie[0]->plot;
			
		//$this->load->view('welcome_message');	
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */

## example.php ##

<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El ActÓr</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
</movies>
XML;
?> 
=> 첨부2(php5.zip) 소스 참조







첨부파일 php4.zip (4.8 KB)
php5.zip (825 Bytes)
 다음글 [예제] 폼검증 파일업로드 동시사용 (6)
 이전글 [팁] CI를 모듈화 처리한다 Matchbox (8)

댓글

변종원(웅파) / 2009/09/02 11:23:36 / 추천 0

덧붙여서 php 5.2 버전부터 xpath query를 사용할 수 있는데 정말 편합니다.
xml 내용중에서 원하는 내용만 뽑아내기.

ci세상 / 2009/09/02 12:04:23 / 추천 0
xpath에 대하여 공부를 많이 해보아야 하겠네요 ~~ 노드를 상당히 빨리 찾을 수 있다는것을 다음 블로그 통해서 느껴만 보았습니다. 실전적용은 아직입니다.^^

http://blog.naver.com/loudon23/30064088340
마냐 / 2009/09/02 18:15:08 / 추천 0

xpath ... 파이어폭스 플러그인도 있군요.
https://addons.mozilla.org/ko/firefox/addon/1192

xml 다루기가 수월하겠네요.

변종원(웅파) / 2009/09/02 21:46:57 / 추천 0

설정부분에서 만들어놓은 xml을 파싱해서 상수로 선언하는 부분에서 xpath query를 사용하는데
파폭 플러그인 보니까 정말 편하겠네요. 얼렁 설치해야겠습니다. ^^

ci세상 / 2009/09/02 22:49:05 / 추천 0
아직 써보지는 않았지만 웅파님께서 올려주신 팁에 감사드리는 바입니다.^^

http://codeigniter-kr.org/tip/view/104/page/2/