YesYo.com MintState Forums  
뒤로    YesYo.com MintState BBS > Tech > Ajax & Issue
검색
멤버이름  오토
비밀번호 
 

 
작성일 : 09-08-27 14:54
XML 파싱(xml2array)
 글쓴이 : MintState
조회 : 8,093  
XML 파싱(xml2array)

몇가지 xml parser를 찾아 봤는데 딱 마음에 드는것은 없네요..
아래는 http://www.bin-co.com/php/scripts/xml2array/ 에서 공개된 xml2array 소스입니다.

<?php 
/** 
 * xml2array() will convert the given XML text to an array in the XML structure. 
 * Link: http://www.bin-co.com/php/scripts/xml2array/ 
 * Arguments : $contents - The XML text 
 *                $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value. 
 *                $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance. 
 * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure. 
 * Examples: $array =  xml2array(file_get_contents('feed.xml')); 
 *              $array =  xml2array(file_get_contents('feed.xml', 1, 'attribute')); 
 */ 
function xml2array($contents, $get_attributes=1, $priority = 'tag') { 
    if(!$contents) return array(); 

    if(!function_exists('xml_parser_create')) { 
        //print "'xml_parser_create()' function not found!"; 
        return array(); 
    } 

    //Get the XML parser of PHP - PHP must have this module for the parser to work 
    $parser = xml_parser_create(''); 
    @xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "euc-kr"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss 
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
    xml_parse_into_struct($parser, trim($contents), $xml_values); 
    xml_parser_free($parser); 

    if(!$xml_values) return;//Hmm... 

    //Initializations 
    $xml_array = array(); 
    $parents = array(); 
    $opened_tags = array(); 
    $arr = array(); 

    $current = &$xml_array; //Refference 

    //Go through the tags. 
    $repeated_tag_index = array();//Multiple tags with same name will be turned into an array 
    foreach($xml_values as $data) { 
        unset($attributes,$value);//Remove existing values, or there will be trouble 

        //This command will extract these variables into the foreach scope 
        // tag(string), type(string), level(int), attributes(array). 
        extract($data);//We could use the array by itself, but this cooler. 

        $result = array(); 
        $attributes_data = array(); 
         
        if(isset($value)) { 
            if($priority == 'tag') $result = $value; 
            else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode 
        } 

        //Set the attributes too. 
        if(isset($attributes) and $get_attributes) { 
            foreach($attributes as $attr => $val) { 
                if($priority == 'tag') $attributes_data[$attr] = $val; 
                else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr' 
            } 
        } 

        //See tag status and do the needed. 
        if($type == "open") {//The starting of the tag '<tag>' 
            $parent[$level-1] = &$current; 
            if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag 
                $current[$tag] = $result; 
                if($attributes_data) $current[$tag. '_attr'] = $attributes_data; 
                $repeated_tag_index[$tag.'_'.$level] = 1; 

                $current = &$current[$tag]; 

            } else { //There was another element with the same tag name 

                if(isset($current[$tag][0])) {//If there is a 0th element it is already an array 
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result; 
                    $repeated_tag_index[$tag.'_'.$level]++; 
                } else {//This section will make the value an array if multiple tags with the same name appear together 
                    $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array 
                    $repeated_tag_index[$tag.'_'.$level] = 2; 
                     
                    if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well 
                        $current[$tag]['0_attr'] = $current[$tag.'_attr']; 
                        unset($current[$tag.'_attr']); 
                    } 
                } 
                $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1; 
                $current = &$current[$tag][$last_item_index]; 
            } 

        } elseif($type == "complete") { //Tags that ends in 1 line '<tag />' 
            //See if the key is already taken. 
            if(!isset($current[$tag])) { //New Key 
                $current[$tag] = $result; 
                $repeated_tag_index[$tag.'_'.$level] = 1; 
                if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data; 

            } else { //If taken, put all things inside a list(array) 
                if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array... 

                    // ...push the new element into that array. 
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result; 
                     
                    if($priority == 'tag' and $get_attributes and $attributes_data) { 
                        $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data; 
                    } 
                    $repeated_tag_index[$tag.'_'.$level]++; 

                } else { //If it is not an array... 
                    $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value 
                    $repeated_tag_index[$tag.'_'.$level] = 1; 
                    if($priority == 'tag' and $get_attributes) { 
                        if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well 
                             
                            $current[$tag]['0_attr'] = $current[$tag.'_attr']; 
                            unset($current[$tag.'_attr']); 
                        } 
                         
                        if($attributes_data) { 
                            $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data; 
                        } 
                    } 
                    $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken 
                } 
            } 

        } elseif($type == 'close') { //End of tag '</tag>' 
            $current = &$parent[$level-1]; 
        } 
    } 
     
    return($xml_array); 
}

?>



추가 :
괜찮은 소스가 있어서 추가 합니다. 클레스로 되어있네요.
http://www.phpbuilder.com/snippet/detail.php?id=868&type=snippet

<?php
/**
*	=============================================================
*	The simple class for parse XML content to   PHP array.
*	Version     :   1.0.1
*	Last Upd    :   Thu Jan 30 14:51:18 GMT 2003
*	Created By  :   Heiko   Mundle [hmundle1@gmx.net]
*	New Features:   reads all attributes of a xml tag
*					doesn't overwrite tags with same tag name (and same tag attribute)
*
*	The simple class for parse XML content to   PHP array.
*	Version     :   1.0
*	Last Upd    :   Thu Oct 24 11:51:18 GMT 2002
*	Created By  :   Phairote Charoen [phairotec@hotmail.com]
*	=============================================================
*	This XML content must   specific some   attribute   for the key of record.
*	<root>
*		<item    id="1" class="myclass">
*			<name>xxxx</name>
*			<price>100</price>
*		</item>
*		more content
*	</root>
*
*	Result :
*Array
*(
* [0] => Array
*   (
*     [name] => root
*     [attr] => Array
*       (
*       )
*     [cont] => Array
*       (
*         [0] => Array
*           (
*             [name] => item
*             [attr] => Array
*               (
*                 [id] => 1
*                 [class] => myclass
*               )
*             [cont] => Array
*               (
*                 [0] => Array
*                   (
*                     [name] => name
*                     [attr] => Array
*                       (
*                       )
*                     [cont] => Array
*                       (
*                         [0] => xxxx
*                       )
*                   )
*                 [1] => Array
*                   (
*                     [name] => price
*                     [attr] => Array
*                       (
*                       )
*                     [cont] => Array
*                       (
*                         [0] => 100
*                       )
*                   )
*               )
*           )
*         [1] => more content
*       )
*   )
*)
*
*/
define(WRITE_FLAG_NOT_NULL, 1);
define(WRITE_FLAG_NULL, 2);
define(WRITE_FLAG_EMPTY_ARRAY, 4);

class   Xml2Array
{
	/**
	*   The XML file name( can be   also real   path and web url ).
	*/
	var $xmlFile;
	/**
	*   The array   result from this class.
	*/
	var $arrResult  =   array();
	/**
	*   The key stack, I keep   each tag name   of each level   to stack array.
	*/
	var $keyStack   =   array();
	/**
	*   Deep of stack   array.
	*/
	var $stackIndex =   0;

	/**
	*   The XML Parser ..  
	*/
	var $parser;

	/**
	*   The constructor.
	*   @xmlFile (string)   -   XML source file name
	*/
	function Xml2Array($xmlFile)
	{
		$this->xmlFile   =   $xmlFile;
		$this->parser    =   xml_parser_create();
		xml_set_object($this->parser,    &$this);
		xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING,    false);
		xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE,true);
		xml_set_element_handler($this->parser, "startElement", "endElement");
		xml_set_character_data_handler($this->parser,    "characterData");
	}

	/**
	*   start   element handler
	*/
	function startElement($parser, $name,   $atts)
	{
		$name   =   trim($name);

		$txt = "\$arr   =   "   .   $this->getArrayName($this->stackIndex) . ";";
		eval($txt);

		$this->keyStack[$this->stackIndex] = count($arr);
		$this->stackIndex++;
		$this->writeData("", WRITE_FLAG_EMPTY_ARRAY);

		if(!empty($name))
		{
			// using the element name   to array key..
			$this->keyStack[$this->stackIndex] = "'name'";
			$this->stackIndex++;
			$this->writeData($name,  WRITE_FLAG_NULL);
			$this->stackIndex--;

			$this->keyStack[$this->stackIndex] = "'attr'";
			$this->stackIndex++;
			if(!empty($atts))
			{
				// using the attributes to array key..
				foreach($atts   as $attKey =>    $attVal)
				{
					$this->keyStack[$this->stackIndex] = "'".$attKey."'";
					$this->stackIndex++;
					$this->writeData($attVal,    WRITE_FLAG_NULL);
					$this->stackIndex--;
				}
			}
			else
			{
				$this->writeData("", WRITE_FLAG_EMPTY_ARRAY);
			}
			$this->stackIndex--;

			$this->keyStack[$this->stackIndex] = "'cont'";
			$this->stackIndex++;
		}
	}

	/**
	*   end element handler
	*/
	function endElement($parser, $name)
	{
		$this->stackIndex-=2;
	}
     
	/**
	*   none tag element handler
	*/
	function characterData($parser, $data)
	{
		$data   =   trim($data);
		if(!empty($data))
		{
			$txt = "\$arr   =   "   .   $this->getArrayName($this->stackIndex) . ";";
			eval($txt);

			$this->keyStack[$this->stackIndex] = count($arr);
			$this->stackIndex++;
			$this->writeData($data,  WRITE_FLAG_NOT_NULL);
			$this->stackIndex--;
		}
	}

	/**
	*   write   data in array
	*/
	function writeData($data,   $flag)
	{
		$data   =   trim($data);
		$dataStr = "\"" .   $data   .   "\"";
		switch( $flag   )
		{
			case WRITE_FLAG_NOT_NULL:
			default:
				// stop if there isn't any data
				if(empty($data))
					break;
			case WRITE_FLAG_EMPTY_ARRAY:
				// overwrite default dataStr
				if(empty($data))
					$dataStr = "array()";
			case WRITE_FLAG_NULL:
				$txt = $this->getArrayName($this->stackIndex)."   =   ".$dataStr.";";
				// assign   value   to the result   array   ..
				eval($txt);

			break;
		}
	}

	/**
	*   get name of array
	*/
	function getArrayName($depth)
	{
		if($depth)
		{
			for($i=0;   $i<$depth; $i++)
			{
				$key .= $this->keyStack[$i]."|";
			}
			$key = preg_replace("/\|$/", "]",   $key);
			$key = preg_replace("/\|/", "][",   $key);
			return "\$this->arrResult[".$key;
		}
		else
		{
			return "\$this->arrResult";
		}
	}

	/**
	*   parsing ..
	*/
	function parse()
	{
		if (!($fp   =   fopen($this->xmlFile,    "r")))
		{
			die("Could not open XML input!");
		}

		while   ($data = fread($fp, 4096))
		{
			if (!xml_parse($this->parser,    $data, feof($fp)))
			{
				die(sprintf("XML error: %s at   line %d",   xml_error_string(xml_get_error_code($this->parser)),xml_get_current_line_number($this->parser)));
			}
		}
		fclose($fp);
	}

	/**
	*   @return the result array
	*/
	function getResult()
	{
		return $this->arrResult;
	}

	/**
	*   free the XML parser result ..  
	*/
	function freeResult()
	{
		xml_parser_free($this->parser);
	}

}   // end class   

?>


<?php
	// Testing the clsXml2Array class.
	$xmlFile    = "test.xml";
	$r = new clsXml2Array($xmlFile);
	$r->parse();
	$arrResult = $r->getResult();
	$r->freeResult();

	echo "<PRE>";
	print_r($arrResult);
	echo "</PRE>";
?>
첨부 파일
파일 종류: php lib.Xml2Array.php (6.5K, 0 views)
Rated 0/5 (0%) (0 Votes)

(\__/)
(='.'=)
(")_(")~
Search Infomation by MintState
MintState

 
 

Total 32
번호 제   목 글쓴이 날짜 조회
32 Swiper - Most Modern Mobile Touch Slider MintState 02-02 1513
31 [jquery] 스와프 가능한 이미지 배너 - OWL Carousel MintState 05-30 2837
30 구글맵 api 편리하기 사용가능하게 해주는 gmaps.js MintState 03-04 4322
29 jQuery - FullCalendar MintState 03-21 5916
28 jquery - select box MintState 11-25 5884
27 Wowza Streaming Server MintState 09-19 7650
26 jQuery Datepicker MintState 06-23 6766
25 Using the Twitter Search API MintState 06-01 4729
24 Twitter Search using the Twitter API and PHP MintState 06-01 5404
23 PHP Twitter Search API MintState 06-01 5289
22 모바일(iOS 용) 터치 관련 jQuery 플러그인 MintState 04-18 6332
21 GEO <-> KATEC 변환 MintState 02-21 5514
20 Twitter API 링크 MintState 07-13 7758
19 jQuery Simple WYSIWYG Editor MintState 07-02 8230
18 Prototype.js 의 Object 객체의 메소드 MintState 01-19 6540
17 prototype.js 를 이용한 JSON사용 예제 MintState 01-19 10460
16 php 내장함수을 이용한 XML Parser MintState 08-27 8452
15 XML 파싱(xml2array) MintState 08-27 8094
14 ajax의 xml을 javascript 객체형식으로 자동 변환 MintState 07-07 8364
13 Google Maps API (1) MintState 03-18 14088
 1  2  
모바일 버전으로 보기
CopyRight ©2004 - 2017, YesYo.com MintState. ™