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
34 """this constructs an alternative fixtag procedure that will use appropriate names for namespaces..."""
35 def fixtag(tag, namespaces):
36 """given a decorated tag (of the form {uri}tag), return prefixed tag and namespace declaration, if any"""
37 if isinstance(tag, ElementTree.QName):
38 tag = tag.text
39 namespace_uri, tag = tag[1:].split("}", 1)
40 prefix = namespaces.get(namespace_uri)
41 if prefix is None:
42 if namespace_uri in namespacemap:
43 prefix = namespacemap[namespace_uri]
44 else:
45 prefix = "ns%d" % len(namespaces)
46 namespaces[namespace_uri] = prefix
47 xmlns = ("xmlns:%s" % prefix, namespace_uri)
48 else:
49 xmlns = None
50 return "%s:%s" % (prefix, tag), xmlns
51 return fixtag
52
60
62 """simple wrapper for xml objects"""
71 - def getchild(self, searchtag, tagclass=None):
72 """get a child with the given tag name"""
73 if tagclass is None: tagclass = XMLWrapper
74 for childobj in self.obj.getiterator():
75
76 if childobj == self.obj: continue
77 childns, childtag = splitnamespace(childobj.tag)
78 if childtag == searchtag:
79 child = tagclass(childobj)
80 return child
81 raise KeyError("could not find child with tag %r" % searchtag)
82 - def getchildren(self, searchtag, tagclass=None, excludetags=[]):
83 """get all children with the given tag name"""
84 if tagclass is None: tagclass = XMLWrapper
85 childobjects = []
86 for childobj in self.obj.getiterator():
87
88 if childobj == self.obj: continue
89 childns, childtag = splitnamespace(childobj.tag)
90 if childtag == searchtag:
91 childobjects.append(childobj)
92 children = [tagclass(childobj) for childobj in childobjects]
93 return children
94 - def gettext(self, searchtag):
95 """get some contained text"""
96 return self.getchild(searchtag).obj.text
97 - def getxml(self, encoding=None):
98 return ElementTree.tostring(self.obj, encoding)
99 - def getplaintext(self, excludetags=[]):
100 text = ""
101 if self.obj.text != None: text += self.obj.text
102 for child in self.obj._children:
103 simplechild = XMLWrapper(child)
104 if simplechild.tag not in excludetags:
105 text += simplechild.getplaintext(excludetags)
106 if self.obj.tail != None: text += self.obj.tail
107 return text
109 """get some contained values..."""
110 values = [child.obj.text for child in self.getchildren(searchtag)]
111 return values
113 """return a representation of the object"""
114 return self.tag+':'+repr(self.__dict__)
116 """gets an attribute of the tag"""
117 return self.attrib[attrname]
118 - def write(self, file, encoding="UTF-8"):
119 """writes the object as XML to a file..."""
120 e = ElementTree.ElementTree(self.obj)
121 e.write(file, encoding)
122
124 parser = ElementTree.XMLTreeBuilder()
125 parser.feed(xmlstring)
126 return parser.close()
127
129 return ElementTree.Element(tag, attrib, **extraargs)
130