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:
74 tagclass = XMLWrapper
75 for childobj in self.obj.getiterator():
76
77 if childobj == self.obj:
78 continue
79 childns, childtag = splitnamespace(childobj.tag)
80 if childtag == searchtag:
81 child = tagclass(childobj)
82 return child
83 raise KeyError("could not find child with tag %r" % searchtag)
84 - def getchildren(self, searchtag, tagclass=None, excludetags=[]):
85 """get all children with the given tag name"""
86 if tagclass is None:
87 tagclass = XMLWrapper
88 childobjects = []
89 for childobj in self.obj.getiterator():
90
91 if childobj == self.obj:
92 continue
93 childns, childtag = splitnamespace(childobj.tag)
94 if childtag == searchtag:
95 childobjects.append(childobj)
96 children = [tagclass(childobj) for childobj in childobjects]
97 return children
98 - def gettext(self, searchtag):
99 """get some contained text"""
100 return self.getchild(searchtag).obj.text
101 - def getxml(self, encoding=None):
102 return ElementTree.tostring(self.obj, encoding)
103 - def getplaintext(self, excludetags=[]):
104 text = ""
105 if self.obj.text != None:
106 text += self.obj.text
107 for child in self.obj._children:
108 simplechild = XMLWrapper(child)
109 if simplechild.tag not in excludetags:
110 text += simplechild.getplaintext(excludetags)
111 if self.obj.tail != None:
112 text += self.obj.tail
113 return text
115 """get some contained values..."""
116 values = [child.obj.text for child in self.getchildren(searchtag)]
117 return values
119 """return a representation of the object"""
120 return self.tag+':'+repr(self.__dict__)
122 """gets an attribute of the tag"""
123 return self.attrib[attrname]
124 - def write(self, file, encoding="UTF-8"):
125 """writes the object as XML to a file..."""
126 e = ElementTree.ElementTree(self.obj)
127 e.write(file, encoding)
128
130 parser = ElementTree.XMLTreeBuilder()
131 parser.feed(xmlstring)
132 return parser.close()
133
135 return ElementTree.Element(tag, attrib, **extraargs)
136