Esempio n. 1
0
def obj2xml(root, contents, schema=None, namespace=None):
    kwargs = namespace and {'namespace': namespace} or {}
    if isinstance(contents, (list, tuple)):
        for item in contents:
            element = et.SubElement(root, 'item', **kwargs)
            obj2xml(
                element,
                item,
                schema and schema.element_type,
                namespace
            )
    elif isinstance(contents, dict):
        fields = schema and dict((f.name, f.type) for f in schema.fields) or {}
        for name, item in contents.iteritems():
            element = et.SubElement(root, name, **kwargs)
            obj2xml(
                element,
                item,
                fields.get(name),
                namespace
            )
    elif contents is not None:
        root.text = \
            schema and schema.serialize(contents) or Type.serialize(contents)
    elif contents is None:
        root.set(NIL, 'true')
    return root
Esempio n. 2
0
def obj2xml(root, contents, schema=None, namespace=None):
    kwargs = namespace and {'namespace': namespace} or {}
    if isinstance(contents, (list, tuple)):
        for item in contents:
            element = et.SubElement(root, 'item', **kwargs)
            obj2xml(element, item, schema and schema.element_type, namespace)
    elif isinstance(contents, dict):
        fields = schema and dict((f.name, f.type) for f in schema.fields) or {}
        # Return the fields of the dict in schema order,
        # or arbitrary python dict order if no schema
        field_order = \
            schema and [f.name for f in schema.fields] or contents.keys()
        for name in field_order:
            element = et.SubElement(root, name, **kwargs)
            obj2xml(element, contents.get(name), fields.get(name), namespace)
    elif contents is not None:
        root.text = \
            schema and schema.serialize(contents) or Type.serialize(contents)
    elif contents is None:
        root.set(NIL, 'true')
    return root
Esempio n. 3
0
def obj2xml(root, contents, schema=None, namespace=None):
    kwargs = namespace and {'namespace': namespace} or {}
    if isinstance(contents, (list, tuple)):
        for item in contents:
            element = et.SubElement(root, 'item', **kwargs)
            obj2xml(element, item, schema and schema.element_type, namespace)
    elif isinstance(contents, dict):
        fields = schema and dict((f.name, f.type) for f in schema.fields) or {}
        # Return the fields of the dict in schema order,
        # or arbitrary python dict order if no schema
        field_order = \
            schema and [f.name for f in schema.fields] or contents.keys()
        for name in field_order:
            element = et.SubElement(root, name, **kwargs)
            obj2xml(element, contents.get(name), fields.get(name), namespace)
    elif contents is not None:
        root.text = \
            schema and schema.serialize(contents) or Type.serialize(contents)
    elif contents is None:
        root.set(NIL, 'true')
    return root