Re: XML Serializer Question
| From: | Chuck Burgess | Date: | Thu, 07 May 2009 15:26:54 +0000 |
| Subject: | Re: XML Serializer Question | ||
| References: | 1 2 3 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-29841@lists.php.net to get a copy of this message | ||
On Thu, Apr 30, 2009 at 8:39 AM, Marcelo Barbudas <nostef@gmail.com> wrote:
> Hi Chuck,
>
>>
>>
>> Sorry to bother you, I've been looking through examples online and
>> couldn't find anything.
>>
>> I am using XML_Unserializer to parse something like:
>>
>> <objects type="array">
>>
>> <object>
>> <blaa>d</blaa>
>> </object>
>> </objects>
>>
>> If there is only one object then Unserialize returns just that one
>> object.
>>
>> If there are more it correctly returns an array.
>>
>> I would like Unserializer to return an array even if there is only one
>> object.
>>
>> Is that possible?
>>
>>
>>
>> I believe setting the /XML_UNSERIALIZER_OPTION_COMPLEXTYPE/ option to
>> "array" is what you're looking for...
>>
>
> Thanks for answering.
>
> Ahm if I set that value to array nothing is returned any more. Maybe I am
> doing something wrong...
>
> --
> M.
>
Building a test case myself based on your original XML, it looks to me like
Unserializer works like you are wanting it to without needing any special
setting...
Here's my test file:
require_once 'XML/Unserializer.php';
$unser = new XML_Unserializer();
$xml =
"<objects type=\"array\">
<object>
<blaa>d</blaa>
</object>
</objects>
";
$unser->unserialize($xml);
var_dump($unser->getUnserializedData());
$xml2 =
"<objects type=\"array\">
<object>
<blaa>d</blaa>
</object>
<object>
<blaa>e</blaa>
</object>
</objects>
";
$unser->unserialize($xml2);
var_dump($unser->getUnserializedData());
The results:
array(1) {
["object"]=>
array(1) {
["blaa"]=>
string(1) "d"
}
}
array(1) {
["object"]=>
array(2) {
[0]=>
array(1) {
["blaa"]=>
string(1) "d"
}
[1]=>
array(1) {
["blaa"]=>
string(1) "e"
}
}
}
In both cases, it looks to me like it returns an array. I ran this using
PHP 5.2.4 and XML_Serializer 0.19.2. Do you get a different result, or do
you get the same result and I'm just misunderstanding how you're wanting a
different result?
--
CRB