我通过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解析器。