Symfony序列化器 – 将XML反序列化为对象数组

huangapple go评论54阅读模式
英文:

Symfony serializer - Deserialize xml to array of objects

问题

以下是您要翻译的内容:

php Symfony序列化器 - 将XML反序列化为对象数组

如何将带有属性的XML反序列化为对象数组?

$string = ''<?xml version="1.0" encoding="UTF-8" ?>
<response>
    <item flight="23"/>
    <item flight="24"/>
</response>';

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$metadataAwareNameConverter = new MetadataAwareNameConverter($classMetadataFactory);

$serializer = new Serializer(
    [new ArrayDenormalizer(), new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter)],
    [new XmlEncoder()]
);
$objects = $serializer->deserialize($string, 'App\Entities\Item[]', 'xml');

Item类:

class Item
{
    #[SerializedName('@flight')]
    public string $flight;
}

现在的结果:

array:1 [
  "item" => App\Entities\Item
]
英文:

php Symfony serializer - Deserialize xml to array of objects

How deserialize xml with attributes to array of objects?

$string = '<?xml version="1.0" encoding="UTF-8" ?>
<response>
    <item flight="23"/>
    <item flight="24"/>
</response>';

        $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
        $metadataAwareNameConverter = new MetadataAwareNameConverter($classMetadataFactory);

        $serializer = new Serializer(
            [new ArrayDenormalizer(), new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter)],
            [new XmlEncoder()]
        );
$objects = $serializer->deserialize($string, 'App\Entities\Item[]', 'xml');

Item class:

class Item
{
    #[SerializedName('@flight')]
    public string $flight;
}

Now result:

array:1 [
  "item" => App\Entities\Item
]

答案1

得分: 0

你必须两次输入方括号。

$objects = $serializer->deserialize($string, 'App\Tests\Item[][]', 'xml')

Symfony序列化器 – 将XML反序列化为对象数组

或者以下内容。但基本上是相同的事情。

$xmlContent = '
    <response>
        <item flight="23"/>
        <item flight="24"/>
    </response>
';

class Item
{
    public string $flight;

    /**
     * @param string $flight
     */
    public function __construct(string $flight)
    {
        $this->flight = $flight;
    }

    /**
     * @return string
     */
    public function getFlight(): string
    {
        return $this->flight;
    }
}

$flights = [];
$encoders = [new XmlEncoder()];
$serializer = new Serializer([], $encoders);
$response = $serializer->decode($xmlContent, "xml");
foreach ($response['item'] as $item) {
    $flights[] = new Item($item['@flight']);
}
英文:

You must enter the square brackets twice.

$objects = $serializer->deserialize($string, 'App\Tests\Item[][]', 'xml')

Symfony序列化器 – 将XML反序列化为对象数组

Or the following. But it is basically the same thing.

$xmlContent = '
    <response>
        <item flight="23"/>
        <item flight="24"/>
    </response>
';

class Item
{
    public string $flight;

    /**
     * @param string $flight
     */
    public function __construct(string $flight)
    {
        $this->flight = $flight;
    }

    /**
     * @return string
     */
    public function getFlight(): string
    {
        return $this->flight;
    }
}

$flights = [];
$encoders = [new XmlEncoder()];
$serializer = new Serializer([], $encoders);
$response = $serializer->decode($xmlContent, "xml");
foreach ($response['item'] as $item) {
    $flights[] = new Item($item['@flight']);
}

huangapple
  • 本文由 发表于 2023年5月22日 18:56:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定