검색결과 리스트
글
php5 simple xml을 이용한 xml 파싱 클래스
PHP/PHP5 클래스
2012. 5. 10. 13:57
php5 에서는 simpleXML 이라는 강력하고도 좋은 xml 클래스를 제공합니다.
이 클래스를 활용해서 보다 내가 원하는 형태로 데이타를 찾고
변환 할 수 있도록 클래스를 만듭니다.
#@ xml 파일 내용 /====================================
< ?xml version='1.0' encoding='UTF-8'? > < main> < head> < title>타이틀< /title> < summary>써머리라고 하징< /summary> < /head> < navigation id="3000"> < margin>30< /margin> < item title="메뉴1" image_off="" image_on="" /> < item title="메뉴2" image_off="" image_on="" /> < item title="메뉴3" image_off="" image_on="" /> < item title="메뉴4" image_off="" image_on="" /> < item title="메뉴5" image_off="" image_on="" /> < item title="메뉴6" image_off="" image_on="" /> < /navigation> < background> < image>< /image> < color>< /color> < /background> < /main>
#@ php 클래스 선언 ----
< ?php $sx =new XmlSimple($path.'/modules/main.xml');
#@ xml : head 데이타만 추출하기/==========================
$result=$sx->query('head'); $args1=$sx->fetch($result); print_r($args1); // 결과 Array ( [0] => Array ( [title] => 타이틀 [summary] => 써머리라고 하징 ) )
#@ 네비게이션 메뉴 만 추출하기 /============================
$result=$sx->query('navigation'); $args1=$sx->fetch($result); print_r($args1); // 결과 Array ( [0] => Array ( [attr] => Array ( [id] => 3000 ) [margin] => 30 [item] => Array ( [0] => Array ( [attr] => Array ( [title] => 메뉴1 [image_off] => [image_on] => ) ) [1] => Array ( [attr] => Array ( [title] => 메뉴2 [image_off] => [image_on] => ) ) [2] => Array ( [attr] => Array ( [title] => 메뉴3 [image_off] => [image_on] => ) ) [3] => Array ( [attr] => Array ( [title] => 메뉴4 [image_off] => [image_on] => ) ) [4] => Array ( [attr] => Array ( [title] => 메뉴5 [image_off] => [image_on] => ) ) [5] => Array ( [attr] => Array ( [title] => 메뉴6 [image_off] => [image_on] => ) ) ) ) )
#@ 네비게이션 메뉴 중 item 값만 추출하기 /==================================
$result=$sx->query('navigation/item'); $args1=$sx->fetch($result); print_r($args1); // 결과 Array ( [0] => Array ( [attr] => Array ( [title] => 메뉴1 [image_off] => [image_on] => ) ) [1] => Array ( [attr] => Array ( [title] => 메뉴2 [image_off] => [image_on] => ) ) [2] => Array ( [attr] => Array ( [title] => 메뉴3 [image_off] => [image_on] => ) ) [3] => Array ( [attr] => Array ( [title] => 메뉴4 [image_off] => [image_on] => ) ) [4] => Array ( [attr] => Array ( [title] => 메뉴5 [image_off] => [image_on] => ) ) [5] => Array ( [attr] => Array ( [title] => 메뉴6 [image_off] => [image_on] => ) ) ) ? >
#@ simple xml 를 이용해 데이타 배열로 리턴해 주는 클래스 소스 /==============================
< ?php class XmlSimple { public $xml_obj; private $num_rows = 0; # query 엘리멘트 갯수 #@ void # xpath = xml 파일 경로 public function __construct($xpath){ if(!self::isExists($xpath)) throw new ErrorException('파일이 없어 어데간겨 잘 찾아봐',__LINE__); # 데이타 가져오기 if(!$xml_data = file_get_contents($xpath)) throw new ErrorException('No data',__LINE__); # simple xml $this->xml_obj = new SimpleXMLElement($xml_data); } #@ return xml array # navigation/item public function query($query){ $result=$this->xml_obj->xpath($query); $this->num_rows =count($result); return $result; } #@return array public function fetch($result) { $loop =array(); for($i=0; $i<$this->num_rows; $i++) { // attributes만 있을 수 있음 $attr_count=count($result[$i]->attributes()); if($attr_count>0) { $loop2=&$loop[$i]['attr']; foreach($result[$i]->attributes() as $k2=>$kv){ $loop2[$k2]=strval($kv); } } // child if(count($result[$i])){ foreach($result[$i] as $k=>$v) { // child -> attributes $attr_count=count($v->attributes()); if($attr_count>0) { $loop3=&$loop[$i][$k][]['attr']; foreach($v->attributes() as $k3=>$k3v){ $loop3[$k3]=strval($k3v); } }else{ $loop[$i][$k]=strval($v); } } } } return $loop; } #@ return public function __get($propertyName){ if(property_exists(__CLASS__,$propertyName)){ return $this->{$propertyName}; } } # 로컬 파일인지 체크 protected function isExists($filename){ if(!file_exists($filename)) return false; return true; } } ? >
'PHP > PHP5 클래스' 카테고리의 다른 글
PHP5 Exception 예외처리 사용 방법과 그 종류 (0) | 2012.04.03 |
---|---|
php5 mysqli 디비 접속 클래스 프로그램 및 사용법 (0) | 2012.03.20 |