使用 Symfony2 将 xml 反序列化为对象 [英] Deserialize xml to object with Symfony2

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

问题描述

我通过 API 以 xml 格式收集了一些数据,并希望将其反序列化到对象列表中.我正在使用 Symfony2 并找到 JMSSerializerBundle,但我真的不知道如何使用它.

I collect some data in xml format through an API and would like to deserialize it in an objects list. I'm using Symfony2 and find out JMSSerializerBundle but I do not really know how to use it.

我知道 Sf2 允许序列化/反序列化对象到/从数组,但我正在寻找更具体的东西.例如,对于这个类:

I know that Sf2 allows to serialize/deserialize object to/from array, but I'm looking for something more specific. For example, for this class :

class Screenshot
{
    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var string $url_screenshot
     */
    private $url_screenshot;


    public function __construct($id, $url_screenshot) {
        $this->id = $id;
        $this->url_screenshot = $url_screenshot;
    }


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set url_screenshot
     *
     * @param string $urlScreenshot
     */
    public function setUrlScreenshot($urlScreenshot)
    {
        $this->url_screenshot = $urlScreenshot;
    }

    /**
     * Get url_screenshot
     *
     * @return string 
     */
    public function getUrlScreenshot()
    {
        return $this->url_screenshot;
    }

    /**
     * Serializes the Screenshot object.
     *
     * @return string
     */
    public function serialize()
    {
        return serialize(array(
            $this->id,
            $this->url_screenshot
        ));
    }

    /**
     * Unserializes the Screenshot object.
     *
     * @param string $serialized
     */
    public function unserialize($serialized)
    {
        list(
            $this->id,
            $this->url_screenshot
        ) = unserialize($serialized);
    }

    public function __toString() {
        return "id: ".$this->id
              ."screenshot: ".$this->url_screenshot;
    }
}

我想对这种 xml 进行序列化/反序列化:

I would like serializing/deserializing to/from this kind of xml :

<?xml version="1.0" encoding="UTF-8" ?>
<screenshots>
   <screenshot>
      <id>1</id>
      <url_screenshot>screenshot_url1</url_screenshot>
   </screenshot>
   <screenshot>
      <id>2</id>
      <url_screenshot>screenshot_url2</url_screenshot>
   </screenshot>
   <screenshot>
      <id>3</id>
      <url_screenshot>screenshot_url3</url_screenshot>
   </screenshot>
</screenshots>

我真的很想使用集成/集成到 Sf2 中的东西(平滑"的东西),并且更喜欢避免使用任何自制的 xml 解析器.

I really want to use something integrated/to integrate in Sf2 (something "smooth") and prefer avoiding any homemade xml parsers.

推荐答案

由于 XML 的性质,您想要的东西是不可能的.你总是需要一些东西来翻译对象 -> xml 和 xml -> 对象.

Due to the nature of XML, the exact thing you want, is not possible. You would always need something to translate object -> xml and xml -> object.

我对您的建议是一个作为集合工作并为您处理它的类,将对象列表作为属性保存,可以从 xml 输入创建并从现有对象创建 xml 输出.

My suggestion to you would be a class that works as collection and takes care of it for you, holding the list of objects as property, which can be created from an xml input and create xml output from existing objects.

另一种方法(如果你真的不再需要将它作为 xml 格式)将简单地序列化对象并以这种方式存储它们,或者如果你想要一次全部序列化一个数组(或集合对象).PHP 中的普通 serialize() 和 unserialize() 函数将在那里完成任务.由于它只是数据,因此您甚至不需要类中的序列化和反序列化方法.

An alternativ (if you don't really need to have it as xml anymore) would be to simply serialize the objects and store them that way, or searialize an array (or collection object) if you want them all at once. The plain serialize() and unserialize() functions from PHP will do the trick there. Since it's data only, you don't even need the methods serialize and unserialize in your class.

更新:如果它只是将 XML 带入一个对象,那么 simplexml 已经涵盖了:http://www.php.net/manual/en/function.simplexml-load-string.php

Update: If it's only take the XML into an object, then simplexml already has you covered: http://www.php.net/manual/en/function.simplexml-load-string.php

第二个参数是类名.

引用:您可以使用此可选参数,以便 simplexml_load_string() 将返回指定类的对象.该类应该扩展 SimpleXMLElement 类.

Quote: You may use this optional parameter so that simplexml_load_string() will return an object of the specified class. That class should extend the SimpleXMLElement class.

如果这只是您的目标,那么 simplexml 已经做到了.

If only this is your goal, then simplexml does it already.

更新 2:我已经阅读了更多内容.它不会做你想要的.它接受一个对象并将其序列化为 XML/YAML,然后当然再次从这些序列化状态反转该过程.它不能将一些随机的 XML 文件变成适合您的完美对象.

Update 2: I've read some more into the bundle. It does NOT do what you want. It takes an object and the serializes it into XML/YAML, and then of course reverses that process again from those serialized states. It cannot take some random XML file and turn that into a perfect object for you.

这篇关于使用 Symfony2 将 xml 反序列化为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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