将 XML 反序列化为对象代码不起作用

问题描述 投票:0回答:1

我在 Symfony 中遇到反序列化和 XML 问题。我尝试了普通的 Symfony 序列化器,但它也不起作用。

据我所知,一切似乎都是正确的,但我没有看到问题。

控制器:

<?php

namespace App\Controller;

use App\DataObjects\Dossiers;
use JMS\Serializer\SerializerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/test')]
class TestController extends AbstractController
{
    private $serializer;

    public function __construct(SerializerInterface $serializer)
    {
        $this->serializer = $serializer;
    }

    #[Route('/')]
    public function test(): Response
    {
        $xmlContent = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <Dossiers source="Source" type="myType">
            <Dossier>
                <Name>Name</Name>
                <Description>Description</Description>
            </Dossier>
        </Dossiers>';

        // Deserialisieren Sie den XML-Inhalt
        $dossiers = $this->serializer->deserialize($xmlContent, Dossiers::class, 'xml');

        // Debugging
        echo "Source: " . $dossiers->getSource() . "<br>";
        echo "Type: " . $dossiers->getType() . "<br>";
        echo "Dossiers:<br>";
        foreach ($dossiers->getDossiers() as $dossier) {
            echo "  Name: " . $dossier->getName() . "<br>";
            echo "  Description: " . $dossier->getDescription() . "<br>";
        }

        dd($dossiers);
        return new Response('Parsed XML');
    }
}

应用程序\数据对象\档案:


<?php
namespace App\DataObjects;

use JMS\Serializer\Annotation as JMS;

/**
 * @JMS\XmlRoot("Dossiers")
 */
class Dossiers
{
    /**
     * @JMS\XmlAttribute
     * @JMS\SerializedName("source")
     * @JMS\Type("string")
     */
    private $source;

    /**
     * @JMS\XmlAttribute
     * @JMS\SerializedName("type")
     * @JMS\Type("string")
     */
    private $type;

    /**
     * @JMS\SerializedName("Dossier")
     * @JMS\Type("array<App\DataObjects\Dossier>")
     * @JMS\XmlList(inline = true, entry = "Dossier")
     */
    private $dossiers = [];

    public function getSource()
    {
        return $this->source;
    }

    public function setSource($source)
    {
        $this->source = $source;
    }

    public function getType()
    {
        return $this->type;
    }

    public function setType($type)
    {
        $this->type = $type;
    }

    public function getDossiers()
    {
        return $this->dossiers;
    }

    public function setDossiers(array $dossiers)
    {
        $this->dossiers = $dossiers;
    }
}

应用程序\数据对象\档案:

<?php

namespace App\DataObjects;

use JMS\Serializer\Annotation as JMS;

class Dossier
{
    /**
     * @JMS\XmlElement(cdata=false)
     * @JMS\SerializedName("Name")
     * @JMS\Type("string")
     */
    private $name;

    /**
     * @JMS\XmlElement(cdata=false)
     * @JMS\SerializedName("Description")
     * @JMS\Type("string")
     */
    private $description;

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function setDescription($description)
    {
        $this->description = $description;
    }
}

此刻返回:

Source:
Type:
Dossiers:

 App\DataObjects\Dossiers {#146 ▼
  -source: null
  -type: null
  -dossiers: []
}

我想要的是将 XML 放入 DataObject 中,以便稍后可以使用 DataObject 来处理更复杂的事情。

php xml symfony serialization xml-parsing
1个回答
0
投票

我发现问题了。

JMS 序列化器为您提供了比我使用的更多的注释。因此,我在第一个类的开头添加了 XmlRoot 和 XmlNamespace,以正确处理整个 XML。现在可以了。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <atom:Dossiers xmlns:atom="http://www.w3.org/2005/Atom" source="TestSource" type="TestType">
            <atom:Dossier>
                <atom:Name>Name</atom:Name>
#[JMS\XmlRoot("Dossiers")]
#[JMS\XmlNamespace(uri:"http://www.w3.org/2005/Atom", prefix:"atom")]
class Dossiers
{
    /**
     * @JMS\XmlAttribute
     * @JMS\SerializedName("source")
     * @JMS\Type("string")
     */
    private $source;

    /**
     * @JMS\XmlAttribute
     * @JMS\SerializedName("type")
     * @JMS\Type("string")
     */
    private $type;

    /**
     * @JMS\SerializedName("Dossier")
     * @JMS\Type("App\DataObjects\Dossier")
     * @JMS\XmlElement(cdata=false, namespace="http://www.w3.org/2005/Atom")
     */

© www.soinside.com 2019 - 2024. All rights reserved.