我通过 API 收集一些 xml 格式的数据,并希望将其反序列化到对象列表中。我正在使用 Symfony2 并找出 JMSSerializerBundle 但我真的不知道如何使用它。
我知道 Sf2 允许序列化/反序列化对象到/从数组,但我正在寻找更具体的东西。例如,对于此类:
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 进行序列化/反序列化:
<?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 解析器。