TIP게시판

제목 array2xml,xml2array 라이브러리.
글쓴이 양승현 작성시각 2010/06/08 10:10:12
댓글 : 0 추천 : 0 스크랩 : 0 조회수 : 15654   RSS
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
    include_once("class.array2xml2array.php");

    $admins = array("admin1", "admin2", "admin3");
    $config = array("config" => array(
                            "filepath" => "/tmp",
                            "interval" => 5,
                            "admins"=>$admins));

    $array2XML = new CArray2xml2array();

    // no root
    $array2XML->setArray($admins);
    if ($array2XML->saveArray("admins.xml", "admins")){
        echo "admins array save<br>";
    }

    // one root
    $array2XML->setArray($config);
    if($array2XML->saveArray("config.xml")){
        echo "config array save<br>";
    }
*/
class array2xml {

/*
 * XML Array
 * @var array
 * @access private
 */
private $XMLArray;

/*
 * array is OK
 * @var bool
 * @access private
 */
private $arrayOK;

/*
 * XML file name
 * @var string
 * @access private
 */
private $XMLFile;

/*
 * file is present
 * @var bool
 * @access private
 */
private $fileOK;

/*
 * DOM document instance
 * @var DomDocument
 * @access private
 */
private $doc;

/**
 * Constructor
 * @access public
 */

public function __construct(){

}

/**
 * setteur setXMLFile
 * @access public
 * @param string $XMLFile
 * @return bool
 */

public function setXMLFile($XMLFile){
	if (file_exists($XMLFile)){
		$this->XMLFile = $XMLFile;
		$this->fileOK = true;
	}else{
		$this->fileOK = false;
	}
	return $this->fileOK;
}

/**
 * saveArray
 * @access public
 * @param string $XMLFile
 * @return bool
 */

public function saveArray($XMLFile, $rootName="", $encoding="utf-8", $filepath){
	global $debug;
	$this->doc = new DOMDocument("1.0", $encoding);
	$arr = array();
	
	if (count($this->XMLArray) > 1){
		if ($rootName != ""){
			$root = $this->doc->createElement($rootName);
		}else{
			$root = $this->doc->createElement("root");
			$rootName = "root";
		}
		$arr = $this->XMLArray;
	}else{
		$key = key($this->XMLArray);
		$val = $this->XMLArray[$key];

		if (!is_int($key)){
			$root = $this->doc->createElement($key);
			$rootName = $key;
		}else{
			if ($rootName != ""){
				$root = $this->doc->createElement($rootName);
			}else{
				$root = $this->doc->createElement("root");
				$rootName = "root";
			}
		}
		$arr = $this->XMLArray[$key];
	}

	$root = $this->doc->appendchild($root);
	
	$this->addArray($arr, $root, $rootName, 0);

/*        foreach ($arr as $key => $val){
		$n = $this->doc->createElement($key);
		$nText = $this->doc->createTextNode($val);
		$n->appendChild($nodeText);
		$root->appendChild($n);
	}
*/
	$result = $this->doc->save($filepath.$XMLFile);
	if ($result == 0){
		return false;
	}else{
		return true;
	}
}

/**
 * addArray recursive function
 * @access public
 * @param array $arr
 * @param DomNode &$n
 * @param string $name
 */

function addArray($arr, &$n, $name="", $deps = 0){
	foreach ($arr as $key => $val){
		if (is_int($key)){
			if (strlen($name)>1){
				$newKey = substr($name, 0, strlen($name)-1);
			}else{
				$newKey="item";
			}
		}else{
			$newKey = $key;
		}

		$node = $this->doc->createElement($newKey);
		
		if (is_array($val)){	
			$this->addArray($arr[$key], $node, $key, $deps+1);
		}else{
			$nodeText = $this->doc->createTextNode($val);
			$node->appendChild($nodeText);		
		}	

		$newline = $this->doc->createTextNode("\n");
		$n->insertBefore($newline);
		
		for($i=0; $i <= $deps; $i++){
		$tab = $this->doc->createTextNode("\t");
			$n->insertBefore($tab);
		}
		
		$n->appendChild($node);
	}
}


/**
 * setteur setArray
 * @access public
 * @param array $XMLArray
 * @return bool
 */

public function setArray($XMLArray){
	if (is_array($XMLArray) && count($XMLArray) != 0){
		$this->XMLArray = $XMLArray;

		$this->arrayOK = true;
	}else{
		$this->arrayOK = false;
	}
	return $this->arrayOK;
}

}//End of the class
?>
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * xml2array Class
 *
 * This class converts XML data to array representation.
 *
 * $this->load->library('xml2array');
 * $xml_array = $this->xml2array->parse($xml);
 */
class Xml2array
{
	function parse($xml, $get_attributes = 1, $priority = 'tag')
	{
		$contents = "";
		if (!function_exists('xml_parser_create'))
		{
			return array ();
		}
		$parser = xml_parser_create('');
		if (!($fp = @ fopen($xml, 'rb')))
		{
			return array ();
		}
		while (!feof($fp))
		{
			$contents .= fread($fp, 8192);
		}
		fclose($fp);
		xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
		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...
		$xml_array = array ();
		$parents = array ();
		$opened_tags = array ();
		$arr = array ();
		$current = & $xml_array;
		$repeated_tag_index = array ();
		foreach ($xml_values as $data)
		{
			unset ($attributes, $value);
			extract($data);
			$result = array ();
			$attributes_data = array ();
			if (isset ($value))
			{
				if ($priority == 'tag')
					$result = $value;
				else
					$result['value'] = $value;
			}
			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'
				}
			}
			if ($type == "open")
			{
				$parent[$level -1] = & $current;
				if (!is_array($current) or (!in_array($tag, array_keys($current))))
				{
					$current[$tag] = $result;
					if ($attributes_data)
						$current[$tag . '_attr'] = $attributes_data;
					$repeated_tag_index[$tag . '_' . $level] = 1;
					$current = & $current[$tag];
				}
				else
				{
					if (isset ($current[$tag][0]))
					{
						$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
						$repeated_tag_index[$tag . '_' . $level]++;
					}
					else
					{
						$current[$tag] = array (
							$current[$tag],
							$result
						);
						$repeated_tag_index[$tag . '_' . $level] = 2;
						if (isset ($current[$tag . '_attr']))
						{
							$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")
			{
				if (!isset ($current[$tag]))
				{
					$current[$tag] = $result;
					$repeated_tag_index[$tag . '_' . $level] = 1;
					if ($priority == 'tag' and $attributes_data)
						$current[$tag . '_attr'] = $attributes_data;
				}
				else
				{
					if (isset ($current[$tag][0]) and is_array($current[$tag]))
					{
						$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
					{
						$current[$tag] = array (
							$current[$tag],
							$result
						);
						$repeated_tag_index[$tag . '_' . $level] = 1;
						if ($priority == 'tag' and $get_attributes)
						{
							if (isset ($current[$tag . '_attr']))
							{
								$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')
			{
				$current = & $parent[$level -1];
			}
		}
		return ($xml_array);
	}
}
?>
첨부파일 array_xml.zip (2.5 KB)
 다음글 zip압축관련 라이브러리 (1)
 이전글 json 라이브러리(php version < ph... (1)

댓글

없음