Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing XML array
    text
    copied!<p>I'm using an API that returns XML. I can retrieve one or multiple accounts with the API. I am using the <a href="http://jmsyst.com/libs/serializer" rel="nofollow">Jms Serializer</a> to deserialize this data into simple model classes that will hold the data. </p> <p>An account looks like</p> <pre><code>&lt;account href="https://your-subdomain.recurly.com/v2/accounts/1"&gt; ... &lt;account_code&gt;1&lt;/account_code&gt; &lt;state&gt;active&lt;/state&gt; &lt;username nil="nil"&gt;&lt;/username&gt; &lt;email&gt;verena@example.com&lt;/email&gt; &lt;first_name&gt;Verena&lt;/first_name&gt; &lt;last_name&gt;Example&lt;/last_name&gt; ... &lt;address&gt;...&lt;/address&gt; &lt;/account&gt; </code></pre> <p>I've managed to configure my <code>Account</code> object as follows, which works fine when deserializing:</p> <pre><code>&lt;?php namespace Recurly\Model; use JMS\Serializer\Annotation as JMS; /** * @JMS\XmlRoot("account") */ class Account { /** @JMS\Type("string") */ protected $account_code; /** @JMS\Type("string") */ protected $state; /** @JMS\Type("string") */ protected $username; /** @JMS\Type("string") */ protected $email; /** @JMS\Type("string") */ protected $first_name; /** @JMS\Type("string") */ protected $last_name; /** @JMS\Type("string") */ protected $company_name; /** @JMS\Type("string") */ protected $vat_number; /** @JMS\Type("Model\Address") */ protected $address; /** @JMS\Type("string") */ protected $accept_language; /** @JMS\Type("string") */ protected $hosted_login_token; /** @JMS\Type("DateTime") */ protected $created_at; // getters and setters here } </code></pre> <p>Now, when I get multiple accounts in, it looks like this:</p> <pre><code>&lt;accounts type="array"&gt; &lt;account href="https://your-subdomain.recurly.com/v2/accounts/1"&gt;...&lt;/account&gt; &lt;account href="https://your-subdomain.recurly.com/v2/accounts/2"&gt;...&lt;/account&gt; &lt;account href="https://your-subdomain.recurly.com/v2/accounts/3"&gt;...&lt;/account&gt; &lt;/accounts&gt; </code></pre> <p>I'd like to deserialize this to an array of accounts. However, at the moment, the only way I've found that does the trick is creating a second Model called <code>Accounts</code> that looks like this:</p> <pre><code>&lt;?php namespace Recurly\Model; use JMS\Serializer\Annotation as JMS; class Accounts { /** * @var Account[] * * @JMS\Type("array&lt;Recurly\Model\Account&gt;") * @JMS\XmlList(entry="account") */ protected $accounts; // getters and setters here } </code></pre> <p>When deserializing, I have to pass the correct context:</p> <pre><code>$serializer-&gt;deserialize($rawXml, 'Recurly\Model\Account', 'xml'); // or Recurly\Model\Accounts if I get multiple. </code></pre> <p>I found somewhere (in a SO question or on the JMS Serializer Github) that you can also pass "types" as context, like <code>$serializer-&gt;deserialize($rawXml, 'array&lt;Recurly\Model\Account&gt;', 'xml')</code> but this just results in an empty array... Anyone know if it's possible to deserialze the array without an extra data model? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload