Revision 455
Added by Aaron Marcuse-Kubitza almost 13 years ago
xml_dom.py | ||
---|---|---|
14 | 14 |
|
15 | 15 |
def unescape(str_): return HTMLParser().unescape(str_) |
16 | 16 |
|
17 |
##### |
|
18 |
|
|
17 | 19 |
def get_id(node): return node.getAttribute('id') |
18 | 20 |
|
19 | 21 |
def set_id(node, id_): node.setAttribute('id', id_) |
20 | 22 |
|
23 |
##### |
|
24 |
|
|
21 | 25 |
def is_completely_empty(node): return node.firstChild == None |
22 | 26 |
|
23 | 27 |
def has_one_child(node): |
... | ... | |
37 | 41 |
self.child = self.child.nextSibling |
38 | 42 |
return child |
39 | 43 |
|
44 |
##### |
|
45 |
|
|
40 | 46 |
def is_comment(node): return node.nodeType == Node.COMMENT_NODE |
41 | 47 |
|
42 |
def is_elem(node): return node.nodeType == Node.ELEMENT_NODE |
|
43 |
|
|
44 | 48 |
def is_empty(node): |
45 | 49 |
for child in NodeIter(node): |
46 | 50 |
if not is_comment(child): return False |
47 | 51 |
return True |
48 | 52 |
|
53 |
##### |
|
54 |
|
|
55 |
def is_elem(node): return node.nodeType == Node.ELEMENT_NODE |
|
56 |
|
|
49 | 57 |
class NodeElemIter: |
50 | 58 |
def __init__(self, node): self.child = node.firstChild |
51 | 59 |
|
... | ... | |
100 | 108 |
self.node = self.node.parentNode |
101 | 109 |
return node |
102 | 110 |
|
111 |
##### |
|
112 |
|
|
103 | 113 |
def is_text_node(node): return node.nodeType == Node.TEXT_NODE |
104 | 114 |
|
105 | 115 |
def is_text(node): return has_one_child(node) and is_text_node(node.firstChild) |
... | ... | |
128 | 138 |
self.iter_.next() |
129 | 139 |
return entry |
130 | 140 |
|
141 |
##### |
|
142 |
|
|
131 | 143 |
def set_child(node, name, value): |
132 | 144 |
'''Note: does not remove any existing child of the same name''' |
133 | 145 |
child = node.ownerDocument.createElement(name) |
... | ... | |
142 | 154 |
def replace_with_text(node, str_): |
143 | 155 |
replace(node, node.ownerDocument.createTextNode(str_)) |
144 | 156 |
|
157 |
##### |
|
158 |
|
|
145 | 159 |
def by_tag_name(node, name, last_only=False): |
146 | 160 |
'''last_only optimization returns last matching node''' |
147 | 161 |
children = [] |
... | ... | |
151 | 165 |
if last_only: break |
152 | 166 |
return children |
153 | 167 |
|
168 |
##### |
|
169 |
|
|
154 | 170 |
def create_doc(root='_'): |
155 | 171 |
return minidom.getDOMImplementation().createDocument(None, root, None) |
156 | 172 |
|
173 |
##### |
|
174 |
|
|
157 | 175 |
prettyxml_config = dict(addindent=' ', newl='\n') |
158 | 176 |
toprettyxml_config = prettyxml_config.copy() |
159 | 177 |
util.rename_key(toprettyxml_config, 'addindent', 'indent') |
160 | 178 |
|
179 |
##### |
|
180 |
|
|
161 | 181 |
# minidom modifications |
162 | 182 |
|
163 | 183 |
minidom._write_data = lambda writer, data: writer.write(escape(data)) |
Also available in: Unified diff
xml_dom.py: Improved readability by separating into sections