homepage Welcome to WebmasterWorld Guest from 54.161.240.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
xml set element handler help to build an array
timmy01




msg:4335039
 9:16 am on Jul 5, 2011 (gmt 0)

test xml file:


<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>




// after parsing ARRAY --should-- look like this: (but it doesn't)



Array
(
[123] => Array
(
[1] => Array
(
[name] => A
)
[5] => Array
(
[name] => B
)
)
[456] => Array
(
[7] => Array
(
[name] => C
)
)
)




i am using xml_set_element_handler (php5):



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






//the code i am using



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);



anyone can help me out?

 

httpwebwitch




msg:4335140
 1:00 pm on Jul 5, 2011 (gmt 0)

you're defining

$result_array[$ID][$BLOCK_ID]['name'] = $NAME;

where $BLOCK_ID is going to be an array. You can't have an array as the offset type, it must be a string or integer.

Maybe you meant:

$result_array[$ID][$BLOCK_ID['name']] = $NAME;

I recommend using the built-in SimpleXML class, it parses well and turns your XML into a multidimensional object, easy to iterate with no need to fuss with "CLOSENAME" strings etc.

[php.net...]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved