CXS Script is the Flash & PHP implementation of CXS offering a CXS parser for each programming environment enabling complex data exchange between Flash & PHP. CXS stands for Compact XML Serialization and is an XML application aimed to enable complex data exchange between programming languages. Parsers are offered for Flash and PHP.It is aimed to show:- how the serialization / unserialization works, – what the results look like, – how many signs are used for serialization (traffic) and.
<?php
/*
* THIS PROGRAM IS LICENSED FREE OF CHARGE. THERE IS NO WARRANTY FOR THE PROGRAM,
* TO THE EXTENT PERMITTED BY APPLICABLE LAW. THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE
* THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.
* SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
* REPAIR OR CORRECTION.
* IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO
* MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED BY THE LGP-LICENSE, BE LIABLE TO
* YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
* RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO
* OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* @license LESSER GENERAL PUBLIC LICENSE (LGPL)
* @copyright (c) 2005 download.zehnet.de
*
* CXS (Compact Xml Serialization) adapts php's serialize format to xml structure.
* It is aimed to serve as an effective way for server-client data exchange
* between flash and php (or any other server-side language).
*
* @project : CXS
* @file : CXS parser [php.xml]
* @author : Nicolas Zeh - cxs@download.zehnet.de
* @date : 02/10/2005 - 03/01/2005
* @version $Id: class.cxs.php,v 0.2.20 2005/03/01 15:00:00 $
*
*/
// FLAGS
define('NO_XML_DECL',1);
define('PARSE_STRICT',2);
define('URI_ENCODE',4);
define('NO_UTF8',8);
define('OBJECT_AS_HASH',16);
define('RECORDSET_AS_HASH',32);
class CXS
{
var $FLAGS = 0;
var $mysql_trans = array(
'int' => 'i',
'real' => 'd',
'string' => 's',
'blob' => 's',
'date' => 's',
'datetime' => 's',
'timestamp' => 's',
'time' => 's',
'year' => 's'
);
var $xml_entities_enc = array(
'&' => '&amp;',
'<' => '&lt;',
'>' => '&gt;',
'\''=> '&apos;',
'"' => '&quot;'
);
/*
function: initialize
*/
function CXS(){
$this->_parseFlags(func_get_args(),0);
$this->xml_entities_dec = array_flip(array_reverse($this->xml_entities_enc));
} // end initialize
/*
function: _parseFlags
*/
function _parseFlags( $a,$i ){
for($i; $i<count($a); $i++){
$this->FLAGS |= $a[$i];
}
} // end _parseFlags
/*
function: _xmlEntities
comment first line out if you want to skip if string is matching a CDATA section
*/
function _xmlEntities( $str,$t=false ){
#if(!$t && preg_match('#^<!\[CDATA\[.*\]\]>$#', trim($str)) ) return $str;
return strtr( $str, $t ? $this->xml_entities_dec : $this->xml_entities_enc );
} // end _xmlEntities
/*
function: serialize
*/
function serialize( $obj ){
$f = $this->FLAGS|0;
$this->_parseFlags(func_get_args(),1);
if( $x = $this->_parse($obj) ){
if($this->FLAGS & PARSE_STRICT) $x = '<cxs version="1.0">'. $x .'</cxs>';
if(($this->FLAGS & NO_XML_DECL) ==0)$x = '<?xml version="1.0" encoding="UTF-8"?>'."\n" . $x;
if($this->FLAGS & URI_ENCODE) $x = '&CXS_data='. urlencode($x);
}
$this->FLAGS = $f;
return $x;
} // end serialize
/*
function: serializeAsObject
*/
function serializeAsObject( $obj, $classname ){
$f = $this->FLAGS|0;
$this->_parseFlags(func_get_args(),2);
$obj = (method_exists($obj, 'onSerialize')) ? $obj->onSerialize() : (array)$obj;
if( $x = $this->_createObject('o',$obj,$classname) ){
if($this->FLAGS & PARSE_STRICT) $x = '<cxs version="1.0">'. $x .'</cxs>';
if(($this->FLAGS & NO_XML_DECL) ==0)$x = '<?xml version="1.0" encoding="UTF-8"?>'."\n" . $x;
if($this->FLAGS & URI_ENCODE) $x = '&CXS_data='. urlencode($x);
}
$this->FLAGS = $f;
return $x;
} // end serializeAsObject
/*
function: _createRessource
*/
function _createRessource( $o ){
switch( get_resource_type($o) ){
case('mysql result'):
$numFields = mysql_num_fields($o);
for($i=0; $i<$numFields; $i++){
$col = mysql_field_name($o, $i);
$type = mysql_field_type($o, $i);
$flag = mysql_field_flags($o, $i);
$types[$i] = $this->mysql_trans[$type] ? $this->mysql_trans[$type] : 's';
if( preg_match('#binary#i', $flag) ) $types[$i] = 'c';
$cols[] = $col;
$field[$i] = '<f n="' . utf8_encode($this->_xmlEntities($col)) . '">';
}
while($row = mysql_fetch_row($o)){
for($i=0; $i<$numFields; $i++){
$field[$i] .= $this->_createElement($row[$i], $types[$i]);
}
}
mysql_free_result($o);
return '<r n="' . utf8_encode($this->_xmlEntities(implode(',', $cols))) . '">' . implode('</f>', $field) . '</f></r>';
case('file'): // from php documentation
case('stream'): // but on a test it returned stream
$r = ini_get('auto_detect_line_endings');
if(!$r) @ini_set('auto_detect_line_endings',1);
while(!feof($o)) {
$file .= fgets($o, 4096);
}
if(!$r) @ini_set('auto_detect_line_endings',0);
return $this->_createElement('c', base64_encode($file));
default:
return;
}
} // end _createRessource
/*
function: _createElement
*/
function _createElement( $t,$o='' ){
$x = '<'.$t;
$x .= (is_null($o) || $o==='') ? '/>' : '>'.$o.'</'.$t.'>';
return $x;
} // end _createElement
/*
function: _createObject
*/
function _createObject( $t,$o,$a=false ){
$x = '<'.$t;
if($a) $x .= ' n="'.utf8_encode($this->_xmlEntities($a)).'"';
$x .= '>';
while(list($k,$v) = each($o)){
$x .= $this->_parse($k);
$x .= $this->_parse($v);
}
return $x .'</'.$t.'>';
} // end _createObject
/*
function: _createArray
*/
function _createArray($o){
$x = '<a>';
while(list($k,$v) = each($o)){
$x .= $this->_parse($v);
}
return $x .'</a>';
} // end _createArray
/*
function: _parse
*/
function _parse( $o ){
switch( gettype($o) ){
case('object'):
$n = get_class($o);
$o = (method_exists($o, 'onSerialize')) ? $o->onSerialize() : (array)$o; // or use get_object_vars -> don't know what's better
return ($this->FLAGS & OBJECT_AS_HASH) ? $this->_createObject('h',$o) : $this->_createObject('o',$o,$n);
case('array'):
return (array_values($o) == $o) ? $this->_createArray($o) : $this->_createObject('h',$o);
case('string'):
$o = $this->_xmlEntities($o);
if(($this->FLAGS & NO_UTF8) ==0) $o = utf8_encode($o);
return $this->_createElement('s',$o);
case('integer'):
return $this->_createElement('i',$o);
case('double'):
return $this->_createElement('d',$o);
case('boolean'):
return $this->_createElement('b',$o ?1:0);
case('NULL'):
return $this->_createElement('n');
case('resource'):
return $this->_createRessource($o);
}
return;
} // end _parse
/*
function: unserialize
*/
function unserialize( $str ){
if($this->_parse_xml($str)){
$f = $this->FLAGS|0;
$this->_parseFlags(func_get_args(),1);
$this->x = $this->_unparse( $this->x );
$this->FLAGS = $f;
return $this->x;
}
return NULL;
} // end unserialize
/*
function: unserializeInto
*/
function unserializeInto( $str, &$obj ){
$intObj = &$obj ;
if(!$this->_parse_xml($str)) return false;
$f = $this->FLAGS|0;
$this->_parseFlags(func_get_args(),2);
$re=false; $t = gettype($intObj);
if($this->x && ($this->x['nodeName']=='O' || $this->x['nodeName']=='A')){
if($t=='object'){
$len=count($this->x['childNodes']);
for($i=0; $i<$len; $i=$i+2){
$k = $this->_unparse($this->x['childNodes'][$i]);
$intObj->$k = $this->_unparse($this->x['childNodes'][($i+1)]);
}
$re=true;
} else if($t=='array'){
$len=count($this->x['childNodes']);
for($i=0; $i<$len; $i=$i+2){
$k = $this->_unparse($this->x['childNodes'][$i]);
$intObj[$k] = $this->_unparse($this->x['childNodes'][($i+1)]);
}
$re=true;
}
}
$this->FLAGS = $f;
return $re;
} // end unserializeInto
/*
function: _parseRecordSetAsHash
*/
function _parseRecordSetAsHash( $c,$a ){
$o=array(); $item=array();
$a = explode(',',$a);
$len=count($a);
$flen = count( $c['childNodes'][0]['childNodes'] );
for($i=0; $i<$flen; $i++){
for($j=0; $j<$len; $j++){
$item[$a[$j]] = $this->_uparse($c['childNodes'][$j]['childNodes'][$i]);
}
$o[$i]=$item;
}
return array('columns'=>$a, 'rows'=>$o);
} // end _parseRecordSetAsHash
/*
function: _parseRecordSet
*/
function _parseRecordSet( $c,$a ){
if(!class_exists('CXS_RecordSet')) return NULL;
$a = explode(',',$a); $len=count($a); $item=array();
$o = new CXS_RecordSet($a);
$flen = count( $c['childNodes'][0]['childNodes'] );
for($i=0; $i<$flen; $i++){
for($j=0; $j<$len; $j++){
$item[$a[$j]] = $this->_uparse($c['childNodes'][$j]['childNodes'][$i]);
}
$o.addItem($item);
}
return $o;
} // end _parseRecordSet
/*
function: _parseObject
*/
function _parseObject( $c,$a=false ){
$o = ($a && class_exists($a)) ? new $a : new stdClass; $len=count($c['childNodes']);
for($i=0; $i<$len; $i=$i+2){
$k = $this->_unparse($c['childNodes'][$i]);
$o->$k = $this->_unparse($c['childNodes'][($i+1)]);
}
if(method_exists($o, 'onUnserialize')) $o->onUnserialize();
return $o;
} // end _parseObject
/*
function: _parseHash
*/
function _parseHash( $c ){
$o = array(); $len=count($c['childNodes']);
for($i=0; $i<$len; $i=$i+2){
$k = $this->_unparse($c['childNodes'][$i]);
$o[$k] = $this->_unparse($c['childNodes'][($i+1)]);
}
return $o;
} // end _parseHash
/*
function: _parseArray
*/
function _parseArray( $c ){
$o = array(); $len=count($c['childNodes']);
for($i=0; $i<$len; $i++){
$o[$i] = $this->_unparse($c['childNodes'][$i]);
}
return $o;
} // end _parseArray
/*
function: _unparse
*/
function _unparse( $c ){
switch( $c['nodeName'] ){
case('O'): // object
return ($this->FLAGS & OBJECT_AS_HASH) ? $this->_parseHash($c) : $this->_parseObject($c,$c['attributes']['N']);
case('H'): // associative array
return $this->_parseHash($c);
case('A'): // associative array
return $this->_parseArray($c);
case('S'): // string
return (string) ($this->FLAGS & NO_UTF8) ? $c['content'] : utf8_decode($c['content']);
case('B'): // boolean
return (bool) $c['content'];
case('I'): // integer
return (int) $c['content'];
case('D'): // double, float
return (double) $c['content'];
case('N'): // null
return;
case('C'): // binary
return base64_decode($c['content']);
case('R'): // recordset
return ($this->FLAGS & RECORDSET_AS_HASH) ? $this->_parseRecordSetAsHash($c,$c['attributes']['N']) : $this->_parseRecordSet($c,$c['attributes']['N']);
}
return;
} // end _unparse
/*
function: _parse_xml
*/
function _parse_xml( $str ){
$str = trim($str);
$this->x=array(); $this->_cur=array(-1);
$this->_cxs = ($this->FLAGS & PARSE_STRICT) ? 0 : 1;
$this->_xml_parser = xml_parser_create();
xml_set_object($this->_xml_parser, &$this);
xml_parser_set_option($this->_xml_parser,XML_OPTION_CASE_FOLDING,1);
xml_set_element_handler($this->_xml_parser, "_xml_se", "_xml_ee");
xml_set_character_data_handler($this->_xml_parser, "_xml_cd");
if(xml_parse($this->_xml_parser, $str))
$this->x = $this->x[0];
else
$this->x = NULL;
xml_parser_free($this->_xml_parser);
return $this->x ? true : false;
} // end _parse_xml
/*
function: xml start_element_handler
*/
function _xml_se($parser, $name, $attribs){
if($this->_cxs){
$this->_cur[count($this->_cur)-1]++;
$add = array('nodeName' => $name, 'attributes' => $attribs, 'content' => '', 'childNodes' => array() );
eval('$this->x['. implode('][\'childNodes\'][',$this->_cur) .'] = $add;');
$this->_cur[] = -1;
}
if($name == 'CXS' && $attribs['VERSION']=='1.0')
$this->_cxs=1;
} // end _xml_se
/*
function: xml character_data_handler
*/
function _xml_cd($parser, $data){
if($this->_cxs){
$temp = $this->_cur;
array_pop($temp);
eval('$this->x['. implode('][\'childNodes\'][',$temp) .'][\'content\'] .= $data;');
}
} // end _xml_cd
/*
function: xml end_element_handler
*/
function _xml_ee($parser, $name){
if($this->_cxs)
array_pop($this->_cur);
} // end _xml_ee
} // end class
?>
&nbsp;
/*
* THIS PROGRAM IS LICENSED FREE OF CHARGE. THERE IS NO WARRANTY FOR THE PROGRAM,
* TO THE EXTENT PERMITTED BY APPLICABLE LAW. THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE
* THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.
* SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
* REPAIR OR CORRECTION.
* IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO
* MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED BY THE LGP-LICENSE, BE LIABLE TO
* YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
* RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO
* OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* @license LESSER GENERAL PUBLIC LICENSE (LGPL)
* @copyright (c) 2005 download.zehnet.de
*
* CXS (Compact Xml Serialization) adapts php's serialize format to xml structure.
* It is aimed to serve as an effective way for server-client data exchange
* between flash and php (or any other server-side language).
*
* @project : CXS
* @file : CXS parser [php.xml]
* @author : Nicolas Zeh - cxs@download.zehnet.de
* @date : 02/10/2005 - 03/01/2005
* @version $Id: class.cxs.php,v 0.2.20 2005/03/01 15:00:00 $
*
*/
// FLAGS
define('NO_XML_DECL',1);
define('PARSE_STRICT',2);
define('URI_ENCODE',4);
define('NO_UTF8',8);
define('OBJECT_AS_HASH',16);
define('RECORDSET_AS_HASH',32);
class CXS
{
var $FLAGS = 0;
var $mysql_trans = array(
'int' => 'i',
'real' => 'd',
'string' => 's',
'blob' => 's',
'date' => 's',
'datetime' => 's',
'timestamp' => 's',
'time' => 's',
'year' => 's'
);
var $xml_entities_enc = array(
'&' => '&amp;',
'<' => '&lt;',
'>' => '&gt;',
'\''=> '&apos;',
'"' => '&quot;'
);
/*
function: initialize
*/
function CXS(){
$this->_parseFlags(func_get_args(),0);
$this->xml_entities_dec = array_flip(array_reverse($this->xml_entities_enc));
} // end initialize
/*
function: _parseFlags
*/
function _parseFlags( $a,$i ){
for($i; $i<count($a); $i++){
$this->FLAGS |= $a[$i];
}
} // end _parseFlags
/*
function: _xmlEntities
comment first line out if you want to skip if string is matching a CDATA section
*/
function _xmlEntities( $str,$t=false ){
#if(!$t && preg_match('#^<!\[CDATA\[.*\]\]>$#', trim($str)) ) return $str;
return strtr( $str, $t ? $this->xml_entities_dec : $this->xml_entities_enc );
} // end _xmlEntities
/*
function: serialize
*/
function serialize( $obj ){
$f = $this->FLAGS|0;
$this->_parseFlags(func_get_args(),1);
if( $x = $this->_parse($obj) ){
if($this->FLAGS & PARSE_STRICT) $x = '<cxs version="1.0">'. $x .'</cxs>';
if(($this->FLAGS & NO_XML_DECL) ==0)$x = '<?xml version="1.0" encoding="UTF-8"?>'."\n" . $x;
if($this->FLAGS & URI_ENCODE) $x = '&CXS_data='. urlencode($x);
}
$this->FLAGS = $f;
return $x;
} // end serialize
/*
function: serializeAsObject
*/
function serializeAsObject( $obj, $classname ){
$f = $this->FLAGS|0;
$this->_parseFlags(func_get_args(),2);
$obj = (method_exists($obj, 'onSerialize')) ? $obj->onSerialize() : (array)$obj;
if( $x = $this->_createObject('o',$obj,$classname) ){
if($this->FLAGS & PARSE_STRICT) $x = '<cxs version="1.0">'. $x .'</cxs>';
if(($this->FLAGS & NO_XML_DECL) ==0)$x = '<?xml version="1.0" encoding="UTF-8"?>'."\n" . $x;
if($this->FLAGS & URI_ENCODE) $x = '&CXS_data='. urlencode($x);
}
$this->FLAGS = $f;
return $x;
} // end serializeAsObject
/*
function: _createRessource
*/
function _createRessource( $o ){
switch( get_resource_type($o) ){
case('mysql result'):
$numFields = mysql_num_fields($o);
for($i=0; $i<$numFields; $i++){
$col = mysql_field_name($o, $i);
$type = mysql_field_type($o, $i);
$flag = mysql_field_flags($o, $i);
$types[$i] = $this->mysql_trans[$type] ? $this->mysql_trans[$type] : 's';
if( preg_match('#binary#i', $flag) ) $types[$i] = 'c';
$cols[] = $col;
$field[$i] = '<f n="' . utf8_encode($this->_xmlEntities($col)) . '">';
}
while($row = mysql_fetch_row($o)){
for($i=0; $i<$numFields; $i++){
$field[$i] .= $this->_createElement($row[$i], $types[$i]);
}
}
mysql_free_result($o);
return '<r n="' . utf8_encode($this->_xmlEntities(implode(',', $cols))) . '">' . implode('</f>', $field) . '</f></r>';
case('file'): // from php documentation
case('stream'): // but on a test it returned stream
$r = ini_get('auto_detect_line_endings');
if(!$r) @ini_set('auto_detect_line_endings',1);
while(!feof($o)) {
$file .= fgets($o, 4096);
}
if(!$r) @ini_set('auto_detect_line_endings',0);
return $this->_createElement('c', base64_encode($file));
default:
return;
}
} // end _createRessource
/*
function: _createElement
*/
function _createElement( $t,$o='' ){
$x = '<'.$t;
$x .= (is_null($o) || $o==='') ? '/>' : '>'.$o.'</'.$t.'>';
return $x;
} // end _createElement
/*
function: _createObject
*/
function _createObject( $t,$o,$a=false ){
$x = '<'.$t;
if($a) $x .= ' n="'.utf8_encode($this->_xmlEntities($a)).'"';
$x .= '>';
while(list($k,$v) = each($o)){
$x .= $this->_parse($k);
$x .= $this->_parse($v);
}
return $x .'</'.$t.'>';
} // end _createObject
/*
function: _createArray
*/
function _createArray($o){
$x = '<a>';
while(list($k,$v) = each($o)){
$x .= $this->_parse($v);
}
return $x .'</a>';
} // end _createArray
/*
function: _parse
*/
function _parse( $o ){
switch( gettype($o) ){
case('object'):
$n = get_class($o);
$o = (method_exists($o, 'onSerialize')) ? $o->onSerialize() : (array)$o; // or use get_object_vars -> don't know what's better
return ($this->FLAGS & OBJECT_AS_HASH) ? $this->_createObject('h',$o) : $this->_createObject('o',$o,$n);
case('array'):
return (array_values($o) == $o) ? $this->_createArray($o) : $this->_createObject('h',$o);
case('string'):
$o = $this->_xmlEntities($o);
if(($this->FLAGS & NO_UTF8) ==0) $o = utf8_encode($o);
return $this->_createElement('s',$o);
case('integer'):
return $this->_createElement('i',$o);
case('double'):
return $this->_createElement('d',$o);
case('boolean'):
return $this->_createElement('b',$o ?1:0);
case('NULL'):
return $this->_createElement('n');
case('resource'):
return $this->_createRessource($o);
}
return;
} // end _parse
/*
function: unserialize
*/
function unserialize( $str ){
if($this->_parse_xml($str)){
$f = $this->FLAGS|0;
$this->_parseFlags(func_get_args(),1);
$this->x = $this->_unparse( $this->x );
$this->FLAGS = $f;
return $this->x;
}
return NULL;
} // end unserialize
/*
function: unserializeInto
*/
function unserializeInto( $str, &$obj ){
$intObj = &$obj ;
if(!$this->_parse_xml($str)) return false;
$f = $this->FLAGS|0;
$this->_parseFlags(func_get_args(),2);
$re=false; $t = gettype($intObj);
if($this->x && ($this->x['nodeName']=='O' || $this->x['nodeName']=='A')){
if($t=='object'){
$len=count($this->x['childNodes']);
for($i=0; $i<$len; $i=$i+2){
$k = $this->_unparse($this->x['childNodes'][$i]);
$intObj->$k = $this->_unparse($this->x['childNodes'][($i+1)]);
}
$re=true;
} else if($t=='array'){
$len=count($this->x['childNodes']);
for($i=0; $i<$len; $i=$i+2){
$k = $this->_unparse($this->x['childNodes'][$i]);
$intObj[$k] = $this->_unparse($this->x['childNodes'][($i+1)]);
}
$re=true;
}
}
$this->FLAGS = $f;
return $re;
} // end unserializeInto
/*
function: _parseRecordSetAsHash
*/
function _parseRecordSetAsHash( $c,$a ){
$o=array(); $item=array();
$a = explode(',',$a);
$len=count($a);
$flen = count( $c['childNodes'][0]['childNodes'] );
for($i=0; $i<$flen; $i++){
for($j=0; $j<$len; $j++){
$item[$a[$j]] = $this->_uparse($c['childNodes'][$j]['childNodes'][$i]);
}
$o[$i]=$item;
}
return array('columns'=>$a, 'rows'=>$o);
} // end _parseRecordSetAsHash
/*
function: _parseRecordSet
*/
function _parseRecordSet( $c,$a ){
if(!class_exists('CXS_RecordSet')) return NULL;
$a = explode(',',$a); $len=count($a); $item=array();
$o = new CXS_RecordSet($a);
$flen = count( $c['childNodes'][0]['childNodes'] );
for($i=0; $i<$flen; $i++){
for($j=0; $j<$len; $j++){
$item[$a[$j]] = $this->_uparse($c['childNodes'][$j]['childNodes'][$i]);
}
$o.addItem($item);
}
return $o;
} // end _parseRecordSet
/*
function: _parseObject
*/
function _parseObject( $c,$a=false ){
$o = ($a && class_exists($a)) ? new $a : new stdClass; $len=count($c['childNodes']);
for($i=0; $i<$len; $i=$i+2){
$k = $this->_unparse($c['childNodes'][$i]);
$o->$k = $this->_unparse($c['childNodes'][($i+1)]);
}
if(method_exists($o, 'onUnserialize')) $o->onUnserialize();
return $o;
} // end _parseObject
/*
function: _parseHash
*/
function _parseHash( $c ){
$o = array(); $len=count($c['childNodes']);
for($i=0; $i<$len; $i=$i+2){
$k = $this->_unparse($c['childNodes'][$i]);
$o[$k] = $this->_unparse($c['childNodes'][($i+1)]);
}
return $o;
} // end _parseHash
/*
function: _parseArray
*/
function _parseArray( $c ){
$o = array(); $len=count($c['childNodes']);
for($i=0; $i<$len; $i++){
$o[$i] = $this->_unparse($c['childNodes'][$i]);
}
return $o;
} // end _parseArray
/*
function: _unparse
*/
function _unparse( $c ){
switch( $c['nodeName'] ){
case('O'): // object
return ($this->FLAGS & OBJECT_AS_HASH) ? $this->_parseHash($c) : $this->_parseObject($c,$c['attributes']['N']);
case('H'): // associative array
return $this->_parseHash($c);
case('A'): // associative array
return $this->_parseArray($c);
case('S'): // string
return (string) ($this->FLAGS & NO_UTF8) ? $c['content'] : utf8_decode($c['content']);
case('B'): // boolean
return (bool) $c['content'];
case('I'): // integer
return (int) $c['content'];
case('D'): // double, float
return (double) $c['content'];
case('N'): // null
return;
case('C'): // binary
return base64_decode($c['content']);
case('R'): // recordset
return ($this->FLAGS & RECORDSET_AS_HASH) ? $this->_parseRecordSetAsHash($c,$c['attributes']['N']) : $this->_parseRecordSet($c,$c['attributes']['N']);
}
return;
} // end _unparse
/*
function: _parse_xml
*/
function _parse_xml( $str ){
$str = trim($str);
$this->x=array(); $this->_cur=array(-1);
$this->_cxs = ($this->FLAGS & PARSE_STRICT) ? 0 : 1;
$this->_xml_parser = xml_parser_create();
xml_set_object($this->_xml_parser, &$this);
xml_parser_set_option($this->_xml_parser,XML_OPTION_CASE_FOLDING,1);
xml_set_element_handler($this->_xml_parser, "_xml_se", "_xml_ee");
xml_set_character_data_handler($this->_xml_parser, "_xml_cd");
if(xml_parse($this->_xml_parser, $str))
$this->x = $this->x[0];
else
$this->x = NULL;
xml_parser_free($this->_xml_parser);
return $this->x ? true : false;
} // end _parse_xml
/*
function: xml start_element_handler
*/
function _xml_se($parser, $name, $attribs){
if($this->_cxs){
$this->_cur[count($this->_cur)-1]++;
$add = array('nodeName' => $name, 'attributes' => $attribs, 'content' => '', 'childNodes' => array() );
eval('$this->x['. implode('][\'childNodes\'][',$this->_cur) .'] = $add;');
$this->_cur[] = -1;
}
if($name == 'CXS' && $attribs['VERSION']=='1.0')
$this->_cxs=1;
} // end _xml_se
/*
function: xml character_data_handler
*/
function _xml_cd($parser, $data){
if($this->_cxs){
$temp = $this->_cur;
array_pop($temp);
eval('$this->x['. implode('][\'childNodes\'][',$temp) .'][\'content\'] .= $data;');
}
} // end _xml_cd
/*
function: xml end_element_handler
*/
function _xml_ee($parser, $name){
if($this->_cxs)
array_pop($this->_cur);
} // end _xml_ee
} // end class
?>
&nbsp;