예제 #1
0
    def as_xml(self, tag=None):
        """
        Return an XML element representing ``self``.  The element has
        tag ``tag``, or if that is omitted (or ``None``), the
        ``xml_default_tag`` attribute of ``self``'s class.
        """

        tag = tag or self.xml_default_tag
        instance_td = Instance(self.__class__)  # TODO: Cache this t.d. in class?
        return instance_td.xml_element(self, tag, [tag])
예제 #2
0
def serialize(obj, tag):
    """
    Entry point function to serialize a Python object to an XML element.

    :param obj: Python object to serialize
    :type obj: instance of class having ``xml_descriptor`` attribute

    :return: XML element, as instance of :class:`etree.Element`.
    """

    instance_td = Instance(obj.__class__)
    return instance_td.xml_element(obj, tag)
예제 #3
0
def deserialize(cls, elt, expected_tag):
    """
    Entry point function to deserialize a Python object from an XML element.

    :param cls: class of object to deserialize

    :param elt: XML element
    :type elt: :class:`etree.Element`

    :return: instance of class ``cls``.
    """

    instance_td = Instance(cls)
    return instance_td.extract_from(elt, expected_tag)