Revision 298
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/xml_dom.py | ||
---|---|---|
19 | 19 |
|
20 | 20 |
def is_empty(node): return node.firstChild == None |
21 | 21 |
|
22 |
def is_elem(node): return node.nodeType == Node.ELEMENT_NODE |
|
23 |
|
|
22 | 24 |
class NodeElemIter: |
23 | 25 |
def __init__(self, node): self.child = node.firstChild |
24 | 26 |
|
... | ... | |
26 | 28 |
|
27 | 29 |
def curr(self): |
28 | 30 |
while self.child != None: |
29 |
if self.child.nodeType == Node.ELEMENT_NODE: return self.child
|
|
31 |
if is_elem(self.child): return self.child
|
|
30 | 32 |
self.child = self.child.nextSibling |
31 | 33 |
raise StopIteration |
32 | 34 |
|
... | ... | |
44 | 46 |
|
45 | 47 |
def curr(self): |
46 | 48 |
while self.child != None: |
47 |
if self.child.nodeType == Node.ELEMENT_NODE: return self.child
|
|
49 |
if is_elem(self.child): return self.child
|
|
48 | 50 |
self.child = self.child.previousSibling |
49 | 51 |
raise StopIteration |
50 | 52 |
|
... | ... | |
61 | 63 |
def __iter__(self): return self |
62 | 64 |
|
63 | 65 |
def curr(self): |
64 |
if self.node != None and self.node.nodeType == Node.ELEMENT_NODE: |
|
65 |
return self.node |
|
66 |
if self.node != None and is_elem(self.node): return self.node |
|
66 | 67 |
raise StopIteration |
67 | 68 |
|
68 | 69 |
def next(self): |
... | ... | |
70 | 71 |
self.node = self.node.parentNode |
71 | 72 |
return node |
72 | 73 |
|
74 |
def is_text_node(node): return node.nodeType == Node.TEXT_NODE |
|
75 |
|
|
73 | 76 |
def is_text(node): |
74 |
for child in NodeElemIter(node): return False # has an element node
|
|
75 |
return True
|
|
77 |
child = node.firstChild
|
|
78 |
return child != None and child.nextSibling == None and is_text_node(child)
|
|
76 | 79 |
|
77 | 80 |
def value(node): |
78 | 81 |
if node.firstChild != None: return node.firstChild.nodeValue |
79 | 82 |
else: return node.nodeValue |
80 | 83 |
|
81 | 84 |
def set_value(node, value): |
82 |
if node.nodeType == Node.ELEMENT_NODE: |
|
83 |
node.appendChild(node.ownerDocument.createTextNode(value)) |
|
85 |
if is_elem(node): node.appendChild(node.ownerDocument.createTextNode(value)) |
|
84 | 86 |
else: node.nodeValue = value |
85 | 87 |
|
86 | 88 |
class NodeTextEntryIter: |
... | ... | |
128 | 130 |
|
129 | 131 |
# xml.dom.minidom modifications |
130 | 132 |
|
131 |
def _write_data(writer, data): writer.write(escape(data))
|
|
133 |
xml.dom.minidom._write_data = lambda writer, data: writer.write(escape(data))
|
|
132 | 134 |
|
133 |
xml.dom.minidom._write_data = _write_data |
|
135 |
def __write_opening(self, writer, indent='', addindent='', newl=''): |
|
136 |
writer.write(indent+'<'+self.tagName) |
|
137 |
for attr_idx in xrange(self.attributes.length): |
|
138 |
attr = self.attributes.item(attr_idx) |
|
139 |
writer.write(' '+attr.name+'='+escape(attr.value)) |
|
140 |
writer.write('>'+newl) |
|
141 |
xml.dom.minidom.Element.write_opening = __write_opening |
|
134 | 142 |
|
143 |
def __write_closing(self, writer, indent='', addindent='', newl=''): |
|
144 |
writer.write('</'+self.tagName+'>'+newl) |
|
145 |
xml.dom.minidom.Element.write_closing = __write_closing |
|
146 |
|
|
135 | 147 |
_writexml_orig = xml.dom.minidom.Element.writexml |
136 |
|
|
137 |
def _writexml(self, writer, indent="", addindent="", newl=""): |
|
138 |
if self.firstChild != None and self.firstChild.nextSibling == None\ |
|
139 |
and self.firstChild.nodeType == Node.TEXT_NODE: # a single text node |
|
140 |
writer.write(indent+'<'+self.tagName) |
|
141 |
for attr_idx in xrange(self.attributes.length): |
|
142 |
attr = self.attributes.item(attr_idx) |
|
143 |
writer.write(' '+attr.name+'='+escape(attr.value)) |
|
144 |
writer.write('>'+escape(value(self))+'</'+self.tagName+'>'+newl) |
|
148 |
def __writexml(self, writer, indent='', addindent='', newl=''): |
|
149 |
if is_text(self): |
|
150 |
self.write_opening(writer, indent, addindent, '') # no newline |
|
151 |
writer.write(escape(value(self))) |
|
152 |
self.write_closing(writer, indent, addindent, newl) |
|
145 | 153 |
else: _writexml_orig(self, writer, indent, addindent, newl) |
154 |
xml.dom.minidom.Element.writexml = __writexml |
|
146 | 155 |
|
147 |
xml.dom.minidom.Element.writexml = _writexml |
|
148 |
|
|
149 | 156 |
xml.dom.minidom.Node.__str__ = lambda self: self.toxml() |
Also available in: Unified diff
xml_dom.py: Refactored xml.dom.minidom.Element.writexml() to use new write_opening() and write_closing() methods. Use new functions is_elem() and is_text_node() to determine node type. Corrected definition of a text element to be an element that contains a single text node.