1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """simpler wrapper to the elementtree XML parser"""
23
24 try:
25 from xml.etree import ElementTree
26 except ImportError:
27 from elementtree import ElementTree
28
29 from xml.parsers import expat
30
31 basicfixtag = ElementTree.fixtag
32
33
35 """this constructs an alternative fixtag procedure that will use appropriate names for namespaces..."""
36
37 def fixtag(tag, namespaces):
38 """given a decorated tag (of the form {uri}tag), return prefixed tag and namespace declaration, if any"""
39 if isinstance(tag, ElementTree.QName):
40 tag = tag.text
41 namespace_uri, tag = tag[1:].split("}", 1)
42 prefix = namespaces.get(namespace_uri)
43 if prefix is None:
44 if namespace_uri in namespacemap:
45 prefix = namespacemap[namespace_uri]
46 else:
47 prefix = "ns%d" % len(namespaces)
48 namespaces[namespace_uri] = prefix
49 xmlns = ("xmlns:%s" % prefix, namespace_uri)
50 else:
51 xmlns = None
52 return "%s:%s" % (prefix, tag), xmlns
53 return fixtag
54
55
63
64
66 """simple wrapper for xml objects"""
67
76
77 - def getchild(self, searchtag, tagclass=None):
78 """get a child with the given tag name"""
79 if tagclass is None:
80 tagclass = XMLWrapper
81 for childobj in self.obj.getiterator():
82
83 if childobj == self.obj:
84 continue
85 childns, childtag = splitnamespace(childobj.tag)
86 if childtag == searchtag:
87 child = tagclass(childobj)
88 return child
89 raise KeyError("could not find child with tag %r" % searchtag)
90
91 - def getchildren(self, searchtag, tagclass=None, excludetags=[]):
92 """get all children with the given tag name"""
93 if tagclass is None:
94 tagclass = XMLWrapper
95 childobjects = []
96 for childobj in self.obj.getiterator():
97
98 if childobj == self.obj:
99 continue
100 childns, childtag = splitnamespace(childobj.tag)
101 if childtag == searchtag:
102 childobjects.append(childobj)
103 children = [tagclass(childobj) for childobj in childobjects]
104 return children
105
106 - def gettext(self, searchtag):
107 """get some contained text"""
108 return self.getchild(searchtag).obj.text
109
110 - def getxml(self, encoding=None):
111 return ElementTree.tostring(self.obj, encoding)
112
113 - def getplaintext(self, excludetags=[]):
114 text = ""
115 if self.obj.text != None:
116 text += self.obj.text
117 for child in self.obj._children:
118 simplechild = XMLWrapper(child)
119 if simplechild.tag not in excludetags:
120 text += simplechild.getplaintext(excludetags)
121 if self.obj.tail != None:
122 text += self.obj.tail
123 return text
124
126 """get some contained values..."""
127 values = [child.obj.text for child in self.getchildren(searchtag)]
128 return values
129
131 """return a representation of the object"""
132 return self.tag+':'+repr(self.__dict__)
133
135 """gets an attribute of the tag"""
136 return self.attrib[attrname]
137
138 - def write(self, file, encoding="UTF-8"):
139 """writes the object as XML to a file..."""
140 e = ElementTree.ElementTree(self.obj)
141 e.write(file, encoding)
142
143
145 parser = ElementTree.XMLTreeBuilder()
146 parser.feed(xmlstring)
147 return parser.close()
148
149
151 return ElementTree.Element(tag, attrib, **extraargs)
152