Wednesday, 15 July 2015

PHP SimpleXMLIterator -



PHP SimpleXMLIterator -

i using simplexmliterator parse xml. doing print_r shows simplexmliterator object created xml this: {

[@attributes] => array ( [id] => abc [type] => [timestamp] => 2001-12-12 ) [place] => [name] => pete [dob] => simplexmliterator object ( [dd] => 03 [mm] => 05 [yyyy] => 1973 )

}

which contains dob simplexmliterator object - i.e. kid element of xml tag.

i iterate on object using next code, recurive cant sure how deep each xml tag goes:

function _preparesimplexmliteratorobj(simplexmliterator $xmliterator) { foreach ($xmliterator $key => $value){ // count returns number of children if ($value->count() > 0) { $this->_preparesimplexmliteratorobj($value); } else { $this->assocarray[$key] = (string)$value; } } }

assocarray fellow member variable of class. generates assoc array object.

problem when utilize $value->haschildren() instead of $value->count() false, though $value->count() returns 3 dob? ideas i;m doing wrong? if

if (($value instanceof simplexmliterator))

i false, though dob simplexmliterator object?

this method works wondering haschildren , instanceof?

here sample xml

<tag id="abc" type="a" timestamp="2001-12-12"> <place>us</place> <name>pete</name> <dob> <dd>03</dd> <mm>05</mm> <yyyy>1973</yyyy> </dob> </tag>

thanks

the haschildren method not working because not doing think. key lies in line in documentation:

this method checks whether current simplexmliterator element has sub-elements.

when says "current" means actual simplexmlelement returned current() function if iterating foreach. remember simplexmliterator designed "iterator"

so, when algorithm gets dob node , phone call $value->haschildren() checking if item $value->current() has children. so, in illustration checking whether node /tag/dob/dd has children, not.

php

No comments:

Post a Comment