php로 xml 추출 및 배열에 담기
컨텐츠 정보
- 19,169 조회
- 4 추천
- 목록
본문
수정했는데 알아보기 어렵게 되어서 다시 올립니다.
class Xml
{
var $data;
var $encoding;
var $arr;
var $pointer;
var $index;
//$path : 파일경로
//$filename : 파일명
//$encoding : UTF-8
function xmlInit($path, $filename, $encoding)
{
if (!($fp = @fopen($path."/".$filename, "r")))
{
die("XML : {$filename} 을 열수 없습니다.");
}
$data = fread($fp, filesize($path."/".$filename));
fclose($fp);
$this->encoding = $encoding;
$this->data = $data;
}
function xmlData()
{
$this->index = 0;
$this->pointer[] = &$this->arr;
$xml_parser = xml_parser_create($this->encoding);
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
xml_set_object($xml_parser, &$this);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_parse($xml_parser, $this->data, true );
xml_parser_free($xml_parser);
}
function startElement($parser, $tag, $attribute)
{
foreach($attribute as $name => $value)
{
$value = $this->cleanString($value);
$object->$name = $value;
}
eval( "\\$this->pointer[\\$this->index]->" . $tag . "[] = \\$object;");
eval( "\\$size = sizeof( \\$this->pointer[\\$this->index]->" . $tag . ");" );
eval( "\\$this->pointer[] = &\\$this->pointer[\\$this->index]->" . $tag . "[\\$size-1];" );
$this->index++;
}
function endElement($parser, $tag)
{
array_pop($this->pointer);
$this->index--;
}
function characterData($parser, $data)
{
if(trim($data)) $this->pointer[$this->index] = trim($data);
}
function cleanString($string)
{
return utf8_decode(trim($string));
}
}
추출된 값 형태
stdClass Object
(
[DATA] => Array
(
[0] => stdClass Object
(
[ARTICLE] => Array
(
[0] => stdClass Object
(
[uid] => Array
(
[0] => 8
)
[p_uid] => Array
(
[0] =>
)
[ctgname] => Array
(
[0] => 일반회원
)
[url] => Array
(
[0] => #
)
[depth] => Array
(
[0] =>
)
[level] => Array
(
[0] => 8
)
[access_id] => Array
(
[0] =>
)
[photo] => Array
(
[0] =>
)
)
[1] => stdClass Object
(
[uid] => Array
(
[0] => 9
)
[p_uid] => Array
(
[0] => 8
)
[ctgname] => Array
(
[0] => 가나다찾기
)
[url] => Array
(
[0] => ./member/
)
[depth] => Array
(
[0] => 1
)
[level] => Array
(
[0] => 8
)
[access_id] => Array
(
[0] =>
)
[photo] => Array
(
[0] =>
)
)
[2] => stdClass Object
(
[uid] => Array
(
[0] => 10
)
[p_uid] => Array
(
[0] => 8
)
[ctgname] => Array
(
[0] => 그룹별찾기
)
[url] => Array
(
[0] => ./member/?mode=speed
)
[depth] => Array
(
[0] => 1
)
[level] => Array
(
[0] => 1
)
[access_id] => Array
(
[0] =>
)
[photo] => Array
(
[0] =>
)
)
[3] => stdClass Object
(
[uid] => Array
(
[0] => 11
)
[p_uid] => Array
(
[0] => 8
)
[ctgname] => Array
(
[0] => 그룹(등급)편집
)
[url] => Array
(
[0] => ./member/?mode=group
)
[depth] => Array
(
[0] => 1
)
[level] => Array
(
[0] => 8
)
[access_id] => Array
(
[0] =>
)
[photo] => Array
(
[0] =>
)
)
[4] => stdClass Object
(
[uid] => Array
(
[0] => 12
)
[p_uid] => Array
(
[0] => 8
)
[ctgname] => Array
(
[0] => 출력양식설정
)
[url] => Array
(
[0] => ./member/?mode=fad
)
[depth] => Array
(
[0] => 1
)
[level] => Array
(
[0] => 8
)
[access_id] => Array
(
[0] =>
)
[photo] => Array
(
[0] =>
)
)
[5] => stdClass Object
(
[uid] => Array
(
[0] => 13
)
[p_uid] => Array
(
[0] => 8
)
[ctgname] => Array
(
[0] => 약관및개인보호정책
)
[url] => Array
(
[0] => ./member/?mode=rule
)
[depth] => Array
(
[0] => 1
)
[level] => Array
(
[0] => 8
)
[access_id] => Array
(
[0] =>
)
[photo] => Array
(
[0] =>
)
)
[6] => stdClass Object
(
[uid] => Array
(
[0] => 1
)
[p_uid] => Array
(
[0] =>
)
[ctgname] => Array
(
[0] => 관리자명단
)
[url] => Array
(
[0] => ./administer/
)
[depth] => Array
(
[0] =>
)
[level] => Array
(
[0] => 8
)
[access_id] => Array
 
class Xml
{
var $data;
var $encoding;
var $arr;
var $pointer;
var $index;
//$path : 파일경로
//$filename : 파일명
//$encoding : UTF-8
function xmlInit($path, $filename, $encoding)
{
if (!($fp = @fopen($path."/".$filename, "r")))
{
die("XML : {$filename} 을 열수 없습니다.");
}
$data = fread($fp, filesize($path."/".$filename));
fclose($fp);
$this->encoding = $encoding;
$this->data = $data;
}
function xmlData()
{
$this->index = 0;
$this->pointer[] = &$this->arr;
$xml_parser = xml_parser_create($this->encoding);
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
xml_set_object($xml_parser, &$this);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_parse($xml_parser, $this->data, true );
xml_parser_free($xml_parser);
}
function startElement($parser, $tag, $attribute)
{
foreach($attribute as $name => $value)
{
$value = $this->cleanString($value);
$object->$name = $value;
}
eval( "\\$this->pointer[\\$this->index]->" . $tag . "[] = \\$object;");
eval( "\\$size = sizeof( \\$this->pointer[\\$this->index]->" . $tag . ");" );
eval( "\\$this->pointer[] = &\\$this->pointer[\\$this->index]->" . $tag . "[\\$size-1];" );
$this->index++;
}
function endElement($parser, $tag)
{
array_pop($this->pointer);
$this->index--;
}
function characterData($parser, $data)
{
if(trim($data)) $this->pointer[$this->index] = trim($data);
}
function cleanString($string)
{
return utf8_decode(trim($string));
}
}
추출된 값 형태
stdClass Object
(
[DATA] => Array
(
[0] => stdClass Object
(
[ARTICLE] => Array
(
[0] => stdClass Object
(
[uid] => Array
(
[0] => 8
)
[p_uid] => Array
(
[0] =>
)
[ctgname] => Array
(
[0] => 일반회원
)
[url] => Array
(
[0] => #
)
[depth] => Array
(
[0] =>
)
[level] => Array
(
[0] => 8
)
[access_id] => Array
(
[0] =>
)
[photo] => Array
(
[0] =>
)
)
[1] => stdClass Object
(
[uid] => Array
(
[0] => 9
)
[p_uid] => Array
(
[0] => 8
)
[ctgname] => Array
(
[0] => 가나다찾기
)
[url] => Array
(
[0] => ./member/
)
[depth] => Array
(
[0] => 1
)
[level] => Array
(
[0] => 8
)
[access_id] => Array
(
[0] =>
)
[photo] => Array
(
[0] =>
)
)
[2] => stdClass Object
(
[uid] => Array
(
[0] => 10
)
[p_uid] => Array
(
[0] => 8
)
[ctgname] => Array
(
[0] => 그룹별찾기
)
[url] => Array
(
[0] => ./member/?mode=speed
)
[depth] => Array
(
[0] => 1
)
[level] => Array
(
[0] => 1
)
[access_id] => Array
(
[0] =>
)
[photo] => Array
(
[0] =>
)
)
[3] => stdClass Object
(
[uid] => Array
(
[0] => 11
)
[p_uid] => Array
(
[0] => 8
)
[ctgname] => Array
(
[0] => 그룹(등급)편집
)
[url] => Array
(
[0] => ./member/?mode=group
)
[depth] => Array
(
[0] => 1
)
[level] => Array
(
[0] => 8
)
[access_id] => Array
(
[0] =>
)
[photo] => Array
(
[0] =>
)
)
[4] => stdClass Object
(
[uid] => Array
(
[0] => 12
)
[p_uid] => Array
(
[0] => 8
)
[ctgname] => Array
(
[0] => 출력양식설정
)
[url] => Array
(
[0] => ./member/?mode=fad
)
[depth] => Array
(
[0] => 1
)
[level] => Array
(
[0] => 8
)
[access_id] => Array
(
[0] =>
)
[photo] => Array
(
[0] =>
)
)
[5] => stdClass Object
(
[uid] => Array
(
[0] => 13
)
[p_uid] => Array
(
[0] => 8
)
[ctgname] => Array
(
[0] => 약관및개인보호정책
)
[url] => Array
(
[0] => ./member/?mode=rule
)
[depth] => Array
(
[0] => 1
)
[level] => Array
(
[0] => 8
)
[access_id] => Array
(
[0] =>
)
[photo] => Array
(
[0] =>
)
)
[6] => stdClass Object
(
[uid] => Array
(
[0] => 1
)
[p_uid] => Array
(
[0] =>
)
[ctgname] => Array
(
[0] => 관리자명단
)
[url] => Array
(
[0] => ./administer/
)
[depth] => Array
(
[0] =>
)
[level] => Array
(
[0] => 8
)
[access_id] => Array
 
관련자료
-
링크
댓글 0
등록된 댓글이 없습니다.