反序列化 XML 数组 [英] Deserializing XML array

查看:37
本文介绍了反序列化 XML 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用返回 XML 的 API.我可以使用 API 检索一个或多个帐户.我正在使用 Jms Serializer 将此数据反序列化为将保存数据的简单模型类.

I'm using an API that returns XML. I can retrieve one or multiple accounts with the API. I am using the Jms Serializer to deserialize this data into simple model classes that will hold the data.

帐户看起来像

<account href="https://your-subdomain.recurly.com/v2/accounts/1">
  ...
  <account_code>1</account_code>
  <state>active</state>
  <username nil="nil"></username>
  <email>verena@example.com</email>
  <first_name>Verena</first_name>
  <last_name>Example</last_name>
  ...
  <address>...</address>
</account>

我已经设法按如下方式配置我的 Account 对象,这在反序列化时工作正常:

I've managed to configure my Account object as follows, which works fine when deserializing:

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

现在,当我进入多个帐户时,它看起来像这样:

Now, when I get multiple accounts in, it looks like this:

<accounts type="array">
  <account href="https://your-subdomain.recurly.com/v2/accounts/1">...</account>
  <account href="https://your-subdomain.recurly.com/v2/accounts/2">...</account>
  <account href="https://your-subdomain.recurly.com/v2/accounts/3">...</account>
</accounts>

我想将其反序列化为一组帐户.但是,目前,我发现的唯一方法是创建一个名为 Accounts 的第二个模型,如下所示:

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 Accounts that looks like this:

<?php
namespace Recurly\Model;
use JMS\Serializer\Annotation as JMS;

class Accounts 
{
    /**
     * @var Account[]
     *
     * @JMS\Type("array<Recurly\Model\Account>")
     * @JMS\XmlList(entry="account")
     */
    protected $accounts;

    // getters and setters here
}

反序列化时,我必须传递正确的上下文:

When deserializing, I have to pass the correct context:

$serializer->deserialize($rawXml, 'Recurly\Model\Account', 'xml'); // or Recurly\Model\Accounts if I get multiple.

我在某处(在 SO 问题中或在 JMS Serializer Github 上)发现您还可以将类型"作为上下文传递,例如 $serializer->deserialize($rawXml, 'array<Recurly\Model\Account>', 'xml') 但这只会导致一个空数组......有人知道是否可以在没有额外数据模型的情况下反序列化数组?

I found somewhere (in a SO question or on the JMS Serializer Github) that you can also pass "types" as context, like $serializer->deserialize($rawXml, 'array<Recurly\Model\Account>', 'xml') but this just results in an empty array... Anyone know if it's possible to deserialze the array without an extra data model?

推荐答案

您可以通过调整 xml 结构来使用 $serializer->deserialize($rawXml, 'array', 'xml') (result 和 entry 为默认值).反序列化中有第四个参数 - $context 它可以重新定义默认值,但我找不到方法.

You can use $serializer->deserialize($rawXml, 'array', 'xml') by adjusting xml structure (result and entry are default values). There is 4th parameter in deserialize - $context it can redefine default values, but i can not find the way how.

<result>
    <entry>
        <account_code>1</account_code>
        <state>active</state>
        <username nil="nil"></username>
        <email>verena@exampl.com</email>
        <first_name>Verena</first_name>
        <last_name>Example</last_name>
    </entry>
    <entry>
        <account_code>1</account_code>
        <state>active</state>
        <username nil="nil"></username>
        <email>verena@exampl.com</email>
        <first_name>Verena</first_name>
        <last_name>Example</last_name>
    </entry>
</result>

这篇关于反序列化 XML 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆