|
Project Information
Members
Featured
Downloads
Links
|
Summarylexa-xml-serialization is a lightweight library for serializing PHP objects to XML like it's done in .NET:
You are welcome and recommended to explore the FirstLook tutorial article. Basic conceptsMy xml serializer requires that you mark serializable properties of your classes with doc-comment annotations: /** @XmlAttribute(type, xmlName) */ private $myProp; /** @XmlElement(type, xmlName) */ private $anotherProp; The properties are not required to be public. The lettercase of annotations doesn't matter. Whitespaces are not essential. Parameters are optional: type defaults to string, xmlName defaults to the property name. Parameter values must not be enclosed in quotes. Empty brackets may be omitted. The only requirement for an xml-serializable class is that it must have a default constructor. That is all the constructor arguments must be optional. The xml tag for the root object is customized via the XmlRoot annotation: /** @XmlRoot(burger) */
class BigBacon {
}Supported data typesThe annotation's type parameter may take the following values:
A property may hold values of different types: /** * @XmlElement(string, custom-size) * @XmlElement(int, pixel-size) */ private $size; This means that integer values of the size property will be serialized as <pixel-size> tags, and string values as <custom-size>. APIuse Lexa\XmlSerialization\XmlSerializer; // To serailize DOMDocument XmlSerializer::serialize(object $obj); // To unserialize object XmlSerializer::unserialize(DOMDocument $dom, string $className); // To generate an XML Schema DOMDocument XmlSerializer::generateSchema(string $className); Serializing arrays and collectionsArrays and object-oriented collections (such as ArrayObject or SplStack) are supported. If the serializer determines that the property value is traversable then it will run the foreach loop over it and dump all items into the resulting DOM. Note that no extra container will be created: class Table {
/**
* @XmlElement(TextField, text-field)
* @XmlElement(ComboBoxField, combo-field)
* @XmlElement(BooleanField, checkbox-field)
*/
private $fields;
function __construct() {
$this->fields = new \ArrayObject();
}
}The above code may end up with something like: <Table> <text-field name="name" /> <combo-field name="gender" /> </Table> Arrays and collections must be explicitly initialized in the contstuctor, so that they could be recognized during unserialization process. And they must be empty by default. Nested arrays and associative arrays (with string keys) are not supported. Your feedbackFeel free to email me at bclexa@gmail.com Thank you |