forked from zenoss/pywbem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtupletree.py
70 lines (52 loc) · 2.21 KB
/
tupletree.py
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
#
# (C) Copyright 2003, 2004 Hewlett-Packard Development Company, L.P.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Martin Pool <[email protected]>
"""
tupletree - Convert XML DOM objects to and from tuple trees.
DOM is the standard in-memory representation of XML documents, but it
is very cumbersome for some types of processing where XML encodes
object structures rather than text documents. Direct mapping to Python
classes may not be a good match either.
tupletrees may be created from an in-memory DOM using
dom_to_tupletree(), or from a string using xml_to_tupletree().
Since the Python XML libraries deal mostly with Unicode strings they
are also returned here. If plain Strings are passed in they will be
converted by xmldom.
Each node of the tuple tree is a Python 4-tuple, corresponding to an
XML Element (i.e. <tag>):
(NAME, ATTRS, CONTENTS, None)
The NAME is the name of the element.
The ATTRS are a name-value hash of element attributes.
The CONTENTS is a list of child elements.
The fourth element is reserved.
"""
def ele_to_tupletree(node):
"""Convert a ElementTree to a Tuple"""
name = node.tag
attrs = {}
contents = []
contents.append(node.text)
for child in node:
if str(type(child)) == "<type 'Element'>":
contents.append(ele_to_tupletree(child))
attrs = node.attrib
return (name, attrs, contents, None)
def xml_to_tupletree(xml_string):
"""Parse XML straight into tupletree."""
import xml.etree.cElementTree as ElementTree
ele_xml = ElementTree.fromstring(xml_string)
return ele_to_tupletree(ele_xml)