-
Notifications
You must be signed in to change notification settings - Fork 25
/
xmlhelp.py
125 lines (100 loc) · 2.87 KB
/
xmlhelp.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# The Admin4 Project
# (c) 2013-2022 Andreas Pflug
#
# Licensed under the Apache License,
# see LICENSE.TXT for conditions of usage
import xml.dom.minidom as minidom
class Element(minidom.Element):
def addElement(self, name):
e=self.ownerDocument.createElement(name)
self.appendChild(e)
return e
def addElementIfText(self, name, val):
if val != None:
return self.addElementText(name, val)
return None
def addElementText(self, name, val):
t=minidom.Text()
if val:
t.data=str(val)
else:
t.data=""
t.ownerDocument=self.ownerDocument
e=self.addElement(name)
e.appendChild(t)
return e
def addElementTree(self, el):
if isinstance(el, str):
doc=minidom.parseString(el)
el=doc.documentElement
e=self.ownerDocument.importNode(el, True)
self.appendChild(e)
return e
def setAttribute(self, name, val):
minidom.Element.setAttribute(self, name, val)
return self
def setAttributes(self, attribs):
for key, val in attribs.items():
self.setAttribute(key, str(val))
return self
def getText(self):
pt=[]
for node in self.childNodes:
if node.nodeType == node.TEXT_NODE:
pt.append(node.data)
return "".join(pt)
def getElements(self, name):
return self.getElementsByTagName(name)
def getElement(self, name):
es=self.getElementsByTagName(name)
if es:
return es[0]
return None
def getElementText(self, name, default=None):
n=self.getElement(name)
if n:
return n.getText()
return default
def prettyXml(self):
lines=self.toprettyxml().splitlines(1)
out=[]
for line in lines:
if line.strip():
out.append(line)
return "".join(out)
class DOMImplementation(minidom.DOMImplementation):
def _create_document(self):
return Document()
class Document(minidom.Document):
implementation=DOMImplementation()
@staticmethod
def create(rootName):
doc=Document.implementation.createDocument(None, rootName, None)
return doc.documentElement
@staticmethod
def parseRaw(txt):
doc=minidom.parseString(txt)
return doc.documentElement
@staticmethod
def parse(txt):
doc=Document.implementation.createDocument(None, "none", None)
rootRaw=Document.parseRaw(txt)
root=doc.importNode(rootRaw, True)
doc.rootElement=root
return root
@staticmethod
def parseFile(filename):
doc=Document.implementation.createDocument(None, "none", None)
raw=minidom.parse(filename)
root=doc.importNode(raw.documentElement, True)
doc.rootElement=root
return root
def createElement(self, tagName):
e=Element(tagName)
e.ownerDocument=self
return e
def createElementNS(self, namespaceURI, qualifiedName):
prefix, _localName = minidom._nssplit(qualifiedName)
e = Element(qualifiedName, namespaceURI, prefix)
e.ownerDocument = self
return e