Revision 179b00d4 ncclient/content.py

b/ncclient/content.py
78 78
        if 'tag' in spec:
79 79
            ele = ET.Element(spec.get('tag'), spec.get('attributes', {}))
80 80
            ele.text = spec.get('text', '')
81
            children = spec.get('children', [])
82
            if isinstance(children, dict):
83
                children = [children]
84
            for child in children:
85
                ele.append(XMLConverter.build(child))
81
            ele.tail = spec.get('tail', '')
82
            subtree = spec.get('subtree', [])
83
            # might not be properly specified as list but may be dict
84
            if isinstance(subtree, dict):
85
                subtree = [subtree]
86
            for subele in subtree:
87
                ele.append(XMLConverter.build(subele))
86 88
            return ele
87 89
        elif 'comment' in spec:
88 90
            return ET.Comment(spec.get('comment'))
89 91
        else:
90
            raise ValueError('Invalid tree spec')
92
            raise ContentError('Invalid tree spec')
93
    
94
    @staticmethod
95
    def from_string(xml):
96
        return XMLConverter.parse(ET.fromstring(xml))
97
    
98
    @staticmethod
99
    def parse(root):
100
        return {
101
            'tag': root.tag,
102
            'attributes': root.attrib,
103
            'text': root.text,
104
            'tail': root.tail,
105
            'subtree': [ XMLConverter.parse(child) for child in root.getchildren() ]
106
        }

Also available in: Unified diff