This is a XML encoder that validates the result against an XSD. Works well with the Symfony Serialization Component and the Drupal Serializer.
<?php
$encoder = new \TurnerLabs\ValidatingXmlEncoder\Encoder('rootNodeName', $pathToXsd));
// If the encoded result of $data is not valid according to the XSD,
// \TurnerLabs\ValidatingXmlEncoder\Exception\XsdValidationException is thrown.
$xml = $encoder->encode($data);