CI 코드
| 타로 | |
|---|---|
|
출처: blog.insicdesigns.com simpleXML CodeIgniter 라이브러리 관련정보가 미흡한듯 하여 등록합니다. 원저작물 http://www.phpclasses.org/browse/package/4484.html 이것으로, CI라이브러리용으로 수정하였더군요. 출처링크를 따라가면 모든 기능을 확인할 수 있습니다. 재미있는 것도 있군요. ^^
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Simplexml{
var $result = array();
var $ignore_level = 0;
var $skip_empty_values = false;
var $php_errormsg;
var $evalCode="";
function xml_parse($data){
$php_errormsg="";
$this->result="";
$this->evalCode="";
$values="";
$encoding = 'UTF-8';
$parser = xml_parser_create($encoding);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
$ok = xml_parse_into_struct($parser, $data, $values);
if (!$ok) {
$errmsg = sprintf("XML parse error %d '%s' at line %d, column %d (byte index %d)",
xml_get_error_code($parser),
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser),
xml_get_current_column_number($parser),
xml_get_current_byte_index($parser));
}
xml_parser_free($parser);
return $this->xml_reorganize($values);
}
function xml_reorganize($array)
{
$count = count($array);
$repeat = $this->xml_tags($array);
$repeatedone = false;
$tags = array();
$k = 0;
for ($i = 0; $i < $count; $i++) {
switch ($array[$i]['type']) {
case 'open':
array_push($tags, $array[$i]['tag']);
if ($i > 0 && ($array[$i]['tag'] == $array[$i-1]['tag']) && ($array[$i-1]['type'] == 'close'))
$k++;
if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) {
array_push($tags, '@content');
$this->array_insert(count($tags), $tags, $array[$i]['value'], "open");
array_pop($tags);
}
if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
if (($repeatedone == $array[$i]['tag'] . $array[$i]['level']) && ($repeatedone)) {
array_push($tags, strval($k++));
} else {
$repeatedone = $array[$i]['tag'] . $array[$i]['level'];
array_push($tags, strval($k));
}
}
if (isset($array[$i]['attributes']) && $array[$i]['attributes'] && $array[$i]['level'] != $this->ignore_level) {
array_push($tags, '@attributes');
foreach ($array[$i]['attributes'] as $attrkey => $attr) {
array_push($tags, $attrkey);
$this->array_insert(count($tags), $tags, $attr, "open");
array_pop($tags);
}
array_pop($tags);
}
break;
case 'close':
array_pop($tags);
if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
if ($repeatedone == $array[$i]['tag'] . $array[$i]['level']) {
array_pop($tags);
} else {
$repeatedone = $array[$i + 1]['tag'] . $array[$i + 1]['level'];
array_pop($tags);
}
}
break;
case 'complete':
array_push($tags, $array[$i]['tag']);
if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
if ($repeatedone == $array[$i]['tag'] . $array[$i]['level'] && $repeatedone) {
array_push($tags, strval($k));
} else {
$repeatedone = $array[$i]['tag'] . $array[$i]['level'];
array_push($tags, strval($k));
}
}
if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) {
if (isset($array[$i]['attributes']) && $array[$i]['attributes']) {
array_push($tags, '@content');
$this->array_insert(count($tags), $tags, $array[$i]['value'], "complete");
array_pop($tags);
} else {
$this->array_insert(count($tags), $tags, $array[$i]['value'], "complete");
}
}
if (isset($array[$i]['attributes']) && $array[$i]['attributes']) {
array_push($tags, '@attributes');
foreach ($array[$i]['attributes'] as $attrkey => $attr) {
array_push($tags, $attrkey);
$this->array_insert(count($tags), $tags, $attr, "complete");
array_pop($tags);
}
array_pop($tags);
}
if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
array_pop($tags);
$k++;
}
array_pop($tags);
break;
}
}
eval($this->evalCode);
$last = $this->array_reindex($this->result);
return $last;
}
function array_insert($level, $tags, $value, $type)
{
$temp = '';
for ($c = $this->ignore_level + 1; $c < $level + 1; $c++) {
if (isset($tags[$c]) && (is_numeric(trim($tags[$c])) || trim($tags[$c]))) {
if (is_numeric($tags[$c])) {
$temp .= '[' . $tags[$c] . ']';
} else {
$temp .= '["' . $tags[$c] . '"]';
}
}
}
$this->evalCode .= '$this->result' . $temp . "=\"" . addslashes($value) . "\";//(" . $type . ")\n";
#echo $code. "\n";
}
/**
* Define the repeated tags in XML file so we can set an index
*
* @param array $array
* @return array
*/
function xml_tags($array)
{ $repeats_temp = array();
$repeats_count = array();
$repeats = array();
if (is_array($array)) {
$n = count($array) - 1;
for ($i = 0; $i < $n; $i++) {
$idn = $array[$i]['tag'].$array[$i]['level'];
if(in_array($idn,$repeats_temp)){
$repeats_count[array_search($idn,$repeats_temp)]+=1;
}else{
array_push($repeats_temp,$idn);
$repeats_count[array_search($idn,$repeats_temp)]=1;
}
}
}
$n = count($repeats_count);
for($i=0;$i<$n;$i++){
if($repeats_count[$i]>1){
array_push($repeats,$repeats_temp[$i]);
}
}
unset($repeats_temp);
unset($repeats_count);
return array_unique($repeats);
}
/**
* Converts Array Variable to Object Variable
*
* @param array $arg_array
* @return $tmp
*/
function array2object ($arg_array)
{
if (is_array($arg_array)) {
$keys = array_keys($arg_array);
if(!is_numeric($keys[0])) $tmp = new Xml;
foreach ($keys as $key) {
if (is_numeric($key)) $has_number = true;
if (is_string($key)) $has_string = true;
}
if (isset($has_number) and !isset($has_string)) {
foreach ($arg_array as $key => $value) {
$tmp[] = $this->array2object($value);
}
} elseif (isset($has_string)) {
foreach ($arg_array as $key => $value) {
if (is_string($key))
$tmp->$key = $this->array2object($value);
}
}
} elseif (is_object($arg_array)) {
foreach ($arg_array as $key => $value) {
if (is_array($value) or is_object($value))
$tmp->$key = $this->array2object($value);
else
$tmp->$key = $value;
}
} else {
$tmp = $arg_array;
}
return $tmp; //return the object
}
/**
* Reindexes the whole array with ascending numbers
*
* @param array $array
* @return array
*/
function array_reindex($array)
{
if (is_array($array)) {
if(count($array) == 1 && isset($array[0])){
return $this->array_reindex($array[0]);
}else{
foreach($array as $keys => $items) {
if (is_array($items)) {
if (is_numeric($keys)) {
$array[$keys] = $this->array_reindex($items);
} else {
$array[$keys] = $this->array_reindex(array_merge(array(), $items));
}
}
}
}
}
return $array;
}
}
출처의 소스에 사소한 오류가 있으니 이곳의 소스를 이용하세요. 정상 작동 확인했습니다.sample.xml <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <item> <id>1</id> <name>iPhone</name> <category>Mobile</category> <price>300</price> </item> <item> <id>2</id> <name>iMac</name> <category>Desktop Computers</category> <price>2500</price> </item> <item> <id>3</id> <name>MacBook Pro</name> <category>Mobile PC</category> <price>2000</price> </item> <item> <id>4</id> <name>iTouch</name> <category>Gadgets</category> <price>150</price> </item> <item> <id>5</id> <name>Wii</name> <category>Gaming</category> <price>1250</price> </item> <item> <id>6</id> <name>Time Capsule</name> <category>Acessories</category> <price>1000</price> </item> <item> <id>7</id> <name>Apple TV</name> <category>Gadgets</category> <price>800</price> </item> </products> 기본사용법
function _getXML($fname)
{
$filename = $fname.'.xml';
$xmlfile="./xml/".$filename;
$xmlRaw = file_get_contents($xmlfile);
$this->load->library('simplexml');
$xmlData = $this->simplexml->xml_parse($xmlRaw);
foreach($xmlData['item'] as $row)
{
$result .= '<tr>';
$result .= '<td>'.$row['id'].'</td>';
$result .= '<td>'.$row['name'].'</td>';
$result .= '<td>'.$row['category'].'</td>';
$result .= '<td>$ '.$row['price'].'</td>';
$result .= '</tr>';
}
return $result;
}
|
|
| 번호 | 제 목 | 글쓴이 | 날짜 | 조회 | 추천수 |
|---|---|---|---|---|---|
| 591 | TapBBS 공개합니다~ [7] | KangMin | 2012-04-19 | 670 | 0 |
| 586 | CI에서 sqlite3 연동하기 [0] | 불의회상 | 2012-04-04 | 299 | 0 |
| 580 | CodeIgniter SimpleXML library [2] | 타로 | 2012-02-05 | 798 | 0 |
| 577 | CI 와 Smarty 템플릿의 결합 [4] | 불의회상 | 2012-01-26 | 900 | 0 |
| 573 | Okada Design Blog 소개 [3] | 타로 | 2012-01-12 | 885 | 0 |
| 568 | woctopus 계정관리도구 [2] | milosz | 2012-01-09 | 765 | 0 |
| 566 | 업로드된 이미지 정사각형으로 썸네일 만드는 함수 [3] | 불의회상 | 2012-01-06 | 782 | 0 |
| 554 | 코드 이그나이터를 접하고 처음으로 만들어본 객체 입.. [7] | 내일은 | 2011-11-11 | 2044 | 0 |
| 528 | 모델코드 생성기 [25] | 불의회상 | 2011-10-13 | 1707 | 1 |
| 525 | ci memo + tank_auth [2] | milosz | 2011-10-04 | 1468 | 0 |
| 512 | 포럼소스를 2.0.1 버전에 맞게 수정하였습니다. [2] | 탱크 | 2011-04-02 | 4095 | 2 |
| 509 | CI 메뉴얼 CHM 버전(영문 2.0) [1] | 브라이언 | 2011-03-30 | 1525 | 0 |
| 508 | CI 메뉴얼 CHM 버전 [0] | 브라이언 | 2011-03-30 | 1739 | 0 |
| 494 | ci memo [8] | pam | 2011-02-20 | 2288 | 1 |
| 489 | CI 압축 프로그램 [2] | 준이 | 2010-12-27 | 2024 | 0 |
| 482 | 듬직이님의 헬퍼 ip 부분 추가. [0] | 나이유미 | 2010-11-22 | 2421 | 0 |
| 475 | SELECT() 사용 시 문제점 [2] | 마냐 | 2010-09-16 | 3251 | 0 |
| 473 | CI의 사용자 인증 소스파일 [2] | corean | 2010-09-12 | 3850 | 0 |
| 471 | CI 1.7.2 한글 언어팩 1.1 [1] | cleansugar | 2010-08-26 | 2489 | 0 |
| 470 | Upload 라이브러리 수정본 [0] | sisco | 2010-07-28 | 2707 | 0 |

