Skip to content
JonathanO edited this page Apr 2, 2013 · 5 revisions

XmlMarshaller

The XmlMarshaller providers serialization of PHP objects to/from XML. Some of the annotations are superficially similar in name and function to JAXB annotations.

At present serialization to XML is not supported, and namespace handling is quite broken.

Currently it is configured using either \Doctrine\Common\Annotations the \Weasel\Annotation library, though there is no reason configuration cannot be loaded from elsewhere.

This is a separate implementation to JsonMarshaller, despite the functionality appearing similar. My previous attempts at implementing this marshaller based on my JSON one led to repeated failures when the differences between the two formats' requirements became apparent.

It is likely that this module will eventually be removed, as I suspect a contract-first approach to XML marshalling will be superior.

Usage

Annotate up all the classes in your object model appropriately, then:

// Get the default factory
$factory = new \Weasel\WeaselDefaultAnnotationDrivenFactory();
// or the Doctrine driven one:
$factory = new \Weasel\WeaselDoctrineAnnotationDrivenFactory();

// Get a mapper
$marshaller = $factory->getXmlMapperInstance();

// Unmarshall to a named type
// This could return a derived type of the target class.
$object = $marshaller->readString($xml, $targetClassName);

Annotations

You've got a choice, the Doctrine driven annotations are probably the future though. Both use the same naming, though handling of constants/enums differ. Examples will be provided for both where there are differences.

Doctrine Namespace: \Weasel\XmlMarshaller\Config\DoctrineAnnotations Weasel Namespace: \Weasel\XmlMarshaller\Config\Annotations

  • @XmlAttribute Mark a property/getter/setter whose serialized form is an XML attribute
  • @XmlDiscriminator Specify an atrribute to use as a discriminator when work with inheritance.
  • @XmlDiscriminatorValue Specify the discriminator value for this class.
  • @XmlElement Mark a property/getter/setter whose serialized form is an XML element
  • @XmlElementRef Specify that the element name is not based on the property, but derived at runtime from the actual type.
  • @XmlElementRefs Provide a list of refs, allowing specified element name mappings
  • @XmlElementWrapper The value should be wrapped in another element. Useful for arrays.
  • @XmlRootElement Specify the namespace and root element name for a class that can be used at the root of an XML document. Also used to provide the default name and namespace when using @XmlElementRef
  • @XmlSeeAlso List of classes that are derived from this one, for when working with inheritance.
  • @XmlType Allows specification of a factory method/class and also specific ordering of properties

Types

XmlMarshaller uses the same type system as Annotation. See AnnotationTypes

Clone this wiki locally