Re: XML_Unserializer - is it right?
| From: | Bertrand Mansion | Date: | Fri, 30 Jan 2004 16:55:06 +0000 |
| Subject: | Re: XML_Unserializer - is it right? | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-10442@lists.php.net to get a copy of this message | ||
<maka3d@yahoo.com.br> wrote :
> //The XML_Unserializer does not create the same dataType for
> //the follow two xml.
>
> $xml = "<root id='12'>
> <child name='john' age='23'/>
> <child name='paul' age='24'/>
> </root>";
>
> $xml2 = "<root id='12'>
> <child name='john' age='23'/>
> </root>";
>
> // the only difference between $xml and $xml2 is that the second has only 1
> child
>
> require_once 'XML/Unserializer.php';
> $options = array('complexType'=>'array',
> 'parseAttributes'=>true);
> $tree = & new XML_Unserializer($options);
> $tree->unserialize($xml);
> $data = $tree->getUnserializedData();
> foreach($data['child'] as $child):
> var_dump($child); // the data type is not the same for $xml and $xml2
> endforeach;
>
> // It means that I have always two situations:
> // 1 - when I have only 1 child
> // 2 - when I have 2 or more
> // I don't want to resolve it putting IF every time I have to iterate over
> a child
> // is it a bug, or I'm missing something
This will do:
if (!is_int(key($data['child']))) {
$data['child'] = array($data['child']);
}
Unless you suggest a better solution...
Bertrand Mansion
Mamasam