Welcome, guest | Sign In | My Account | Store | Cart

The script allows you to convert a XML node into an object instance that has the XML node attributes as fields with the given values. The values are converted (when possible):

  • from string to int
  • from string to float
  • or remain as string

You also initially can provide a dictionary of (key,value) to ensure existence of certain fields which might not be provided by the XML node but on the other hand those values might be overwritten by the XML node.

Please have a look at the docstring for the example ...

Python, 83 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Tool for converting an XML node into an object instance.

.. module:: objectify
    :platform: Unix, Windows
    :synopsis: providing conversion for XML nodes.

.. moduleauthor:: Thomas Lehmann

License
=======
Copyright (c) 2014 Thomas Lehmann

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import re


def objectify(node, attributes=None):
    """
    Converting XML node into an object instance.

    Taking the tag name with first letter as upper case
    as the name forgenerating a class derived from object
    with the node attributes as fields and the values as 'default'.

    >>> import xml.etree.ElementTree as ET
    >>> document = ET.fromstring('<test-obj int-val="1" str-val="hello" float-val="1.23"/>')
    >>> instance = objectify(document, {"object-id": "1234"})
    >>> print(instance.__class__.__name__)
    TestObj
    >>> print(instance.object_id)
    1234
    >>> print(instance.int_val)
    1
    >>> print(instance.str_val)
    hello
    >>> print(instance.float_val)
    1.23

    :param node: xml node (from lxml.etree or xml.etree)
    :param attributes: allows providing fields and default values
                       which might be overwritten by the XML node attributes.
    :returns: instance with node attributes as fields
    """
    def convert(attribute_value):
        """
        Convert string to float or int were possible.

        :param attribute_value: string value
        :return: depend on re.match a string, a float or an int value.
        """
        if re.match(r"\d+\.\d+", attribute_value):
            return float(attribute_value)
        if re.match(r"\d+", attribute_value):
            return int(attribute_value)
        return attribute_value

    if None == attributes:
        attributes = {}
    else:
        attributes = (dict([(key.replace("-", "_"), convert(value))
                            for key, value in attributes.items()]))

    attributes.update(dict([(key.replace("-", "_"), convert(value))
                            for key, value in node.attrib.items()]))
    class_name = "".join([entry.title() for entry in node.tag.split("-")])
    return type(class_name, (object,), attributes)()