Rémi ALVADO
2012-04-11 12:43:53 UTC
Hi,
I'm currently trying to use JMSSerializer to read an XML feed coming from a
solr interface. This feed looks something like :
<doc>
<str name="id">foo</str>
<str name="name">bar</str>
<int name="occ">123</int>
<int name="price">9</int>
</doc>
So, basically, the "doc" element contains a list of "str" elements and a list of "int" elements. "str" and "int" are basically the same except that "int" should contain a text node that is an integer where "str" text node can be any string. Pretty common solr practice so far :)
I've created an interface "SimpleType" that is implemented by both "String" and "Integer" classes. "Integer" class looks like ("String" is nearly the same) :
<?php
namespace MyCompanyBundle\Model\SolrResults;
use JMS\SerializerBundle\Annotation\AccessType,
JMS\SerializerBundle\Annotation\XmlRoot,
JMS\SerializerBundle\Annotation\Type,
JMS\SerializerBundle\Annotation\XmlAttribute,
JMS\SerializerBundle\Annotation\SerializedName,
JMS\SerializerBundle\Annotation\Since,
JMS\SerializerBundle\Annotation\XmlValue;
/**
* @AccessType("public_method")
* @XmlRoot("int")
*/
class Integer implements SimpleType {
/**
* @Type("string")
* @XmlAttribute
* @SerializedName("name")
* @Since("1")
*/
protected $name = null;
/**
* @Type("integer")
* @XmlValue
* @Since("1")
*/
protected $value = null;
function __construct($name = null, $value = null) {
$this->setName($name);
$this->setValue($value);
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getValue() {
return $this->value;
}
public function setValue($value) {
$this->value = $value;
}
}
?>
My issue is with the "Document" class that map the "doc" element. I've tried to use a $simpleTypes array to store all my SimpleType elements (whatever there are String or Integer) and to use some JMS Annotations to describe it as an inline XmlList but I can't manage to get it work. Here an implementation of the "Document" class :
<?php
namespace MyCompanyBundle\Model\SolrResults;
use JMS\SerializerBundle\Annotation\AccessType,
JMS\SerializerBundle\Annotation\Accessor,
JMS\SerializerBundle\Annotation\XmlRoot,
JMS\SerializerBundle\Annotation\Type,
JMS\SerializerBundle\Annotation\XmlAttribute,
JMS\SerializerBundle\Annotation\SerializedName,
JMS\SerializerBundle\Annotation\Since,
JMS\SerializerBundle\Annotation\XmlValue,
JMS\SerializerBundle\Annotation\XmlList,
JMS\SerializerBundle\Annotation\Exclude;
/**
* @AccessType("public_method")
* @XmlRoot("doc")
*/
class Document {
/**
* @Type("array<MyCompanyBundle\Model\SolrResults\SimpleType>")
* @XmlList(inline = true)
* @Accessor(getter="getSimpleTypes")
* @Since("1")
*/
protected $simpleTypes = array();
function __construct($simpleTypes = null) {
$this->setSimpleTypes($simpleTypes);
}
public function getSimpleTypes() {
return $this->simpleTypes;
}
public function setSimpleTypes($simpleTypes) {
if (!is_array($simpleTypes)) $simpleTypes = array();
$this->simpleTypes = $simpleTypes;
}
}
?>
So far, what I get when I try to serialize my object is :
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<entry name="foo1"><![CDATA[bar]]></entry>
<entry name="foo2">12345</entry>
</doc>
which is not what I'm expecting. I know that I could use @XmlList(inline =
true, entry= "xxx") where xxx would be nodeName for any elements in my
$simpleTypes array but since I can have different class in this array, I'd
prefer that JMSSerializer can use the XMLRoot value defined in my "Integer"
and "String" classes.
Is there any way to do that or should I have two arrays, one for String
elements and the other for Integer ones ?
Thanks.
Rémi
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com
You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
I'm currently trying to use JMSSerializer to read an XML feed coming from a
solr interface. This feed looks something like :
<doc>
<str name="id">foo</str>
<str name="name">bar</str>
<int name="occ">123</int>
<int name="price">9</int>
</doc>
So, basically, the "doc" element contains a list of "str" elements and a list of "int" elements. "str" and "int" are basically the same except that "int" should contain a text node that is an integer where "str" text node can be any string. Pretty common solr practice so far :)
I've created an interface "SimpleType" that is implemented by both "String" and "Integer" classes. "Integer" class looks like ("String" is nearly the same) :
<?php
namespace MyCompanyBundle\Model\SolrResults;
use JMS\SerializerBundle\Annotation\AccessType,
JMS\SerializerBundle\Annotation\XmlRoot,
JMS\SerializerBundle\Annotation\Type,
JMS\SerializerBundle\Annotation\XmlAttribute,
JMS\SerializerBundle\Annotation\SerializedName,
JMS\SerializerBundle\Annotation\Since,
JMS\SerializerBundle\Annotation\XmlValue;
/**
* @AccessType("public_method")
* @XmlRoot("int")
*/
class Integer implements SimpleType {
/**
* @Type("string")
* @XmlAttribute
* @SerializedName("name")
* @Since("1")
*/
protected $name = null;
/**
* @Type("integer")
* @XmlValue
* @Since("1")
*/
protected $value = null;
function __construct($name = null, $value = null) {
$this->setName($name);
$this->setValue($value);
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getValue() {
return $this->value;
}
public function setValue($value) {
$this->value = $value;
}
}
?>
My issue is with the "Document" class that map the "doc" element. I've tried to use a $simpleTypes array to store all my SimpleType elements (whatever there are String or Integer) and to use some JMS Annotations to describe it as an inline XmlList but I can't manage to get it work. Here an implementation of the "Document" class :
<?php
namespace MyCompanyBundle\Model\SolrResults;
use JMS\SerializerBundle\Annotation\AccessType,
JMS\SerializerBundle\Annotation\Accessor,
JMS\SerializerBundle\Annotation\XmlRoot,
JMS\SerializerBundle\Annotation\Type,
JMS\SerializerBundle\Annotation\XmlAttribute,
JMS\SerializerBundle\Annotation\SerializedName,
JMS\SerializerBundle\Annotation\Since,
JMS\SerializerBundle\Annotation\XmlValue,
JMS\SerializerBundle\Annotation\XmlList,
JMS\SerializerBundle\Annotation\Exclude;
/**
* @AccessType("public_method")
* @XmlRoot("doc")
*/
class Document {
/**
* @Type("array<MyCompanyBundle\Model\SolrResults\SimpleType>")
* @XmlList(inline = true)
* @Accessor(getter="getSimpleTypes")
* @Since("1")
*/
protected $simpleTypes = array();
function __construct($simpleTypes = null) {
$this->setSimpleTypes($simpleTypes);
}
public function getSimpleTypes() {
return $this->simpleTypes;
}
public function setSimpleTypes($simpleTypes) {
if (!is_array($simpleTypes)) $simpleTypes = array();
$this->simpleTypes = $simpleTypes;
}
}
?>
So far, what I get when I try to serialize my object is :
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<entry name="foo1"><![CDATA[bar]]></entry>
<entry name="foo2">12345</entry>
</doc>
which is not what I'm expecting. I know that I could use @XmlList(inline =
true, entry= "xxx") where xxx would be nodeName for any elements in my
$simpleTypes array but since I can have different class in this array, I'd
prefer that JMSSerializer can use the XMLRoot value defined in my "Integer"
and "String" classes.
Is there any way to do that or should I have two arrays, one for String
elements and the other for Integer ones ?
Thanks.
Rémi
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com
You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en