doulan9287 2012-04-11 22:00
浏览 34
已采纳

使用Symfony2将xml反序列化为对象

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.

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

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>

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

  • 写回答

1条回答 默认 最新

  • doupai8095 2012-04-12 07:55
    关注

    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.

    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.

    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.

    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

    The second parameter is class name.

    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.

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么