Forum Moderators: open
<getXML>
<result>
<block>
<name>A</name>
<block_id>1</block_id>
</block>
<block>
<name>B</name>
<block_id>5</block_id>
</block>
<id>123</id>
</result>
<result>
<block>
<name>C</name>
<block_id>7</block_id>
</block>
<id>456</id>
</result>
</getXML>
Array
(
[123] => Array
(
[1] => Array
(
[name] => A
)
[5] => Array
(
[name] => B
)
)
[456] => Array
(
[7] => Array
(
[name] => C
)
)
)
class xml
{
var $parser;
var $block_text;
var $buffer;
function xml()
{
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, "tag_open", "tag_close");
xml_set_character_data_handler($this->parser, "cdata");
$this->block_text = FALSE;
$this->buffer = '';
}
function parse($data)
{
xml_parse($this->parser, $data);
}
function cdata($parser, $cdata)
{
global $alltags;
if ($this->block_text)
$this->buffer .= $cdata;
else
$alltags[] = $cdata;
//var_dump($parser, $cdata);
}
function tag_close($parser, $tag)
{
global $alltags;
if ($this->block_text) {
$alltags[] = $this->buffer;
$this->buffer = '';
}
$alltags[] = 'CLOSE'.$tag;
}
} //end of class xml
for ( $a=0; $a < count($alltags); $a++ )
{
if ( $alltags[$a] == 'CLOSEID' )
{
$ID = $alltags[$a-1];
}
if ( $alltags[$a] == 'CLOSEBLOCK_ID' )
{
$BLOCK_ID[$alltags[$a-1]] = $alltags[$a-1];
}
if ( $alltags[$a] == 'CLOSENAME' )
{
$NAME = $alltags[$a-1];
}
if ( $alltags[$a] == 'CLOSERESULT' )
{
$result_array[$ID] = $BLOCK_ID;
$result_array[$ID][$BLOCK_ID]['name'] = $NAME; //here is when i get the warning: Illegal offset type in
$BLOCK_ID='';
}
}
unset($alltags);