Project

General

Profile

« Previous | Next » 

Revision 2008

xml_dom.py: Added value_node(). Use new value_node() in value() and set_value(). set_value(): If the node already has a value node, reuse it instead of appending a new value node.

View differences:

lib/xml_dom.py
24 24
    if sep != '': return base
25 25
    else: return name
26 26

  
27
##### Replacing a node
28

  
29
def remove(node): node.parentNode.removeChild(node)
30

  
31
def replace(old, new):
32
    '''@param new Node|None'''
33
    if new == None: old.parentNode.removeChild(old)
34
    else: old.parentNode.replaceChild(new, old) # note order reversed
35

  
36
def replace_with_text(node, new):
37
    if util.is_str(new): new = node.ownerDocument.createTextNode(new)
38
    replace(node, new)
39

  
27 40
##### Element node contents
28 41

  
29 42
def is_elem(node): return node.nodeType == Node.ELEMENT_NODE
......
140 153

  
141 154
def is_text(node): return has_one_child(node) and is_text_node(node.firstChild)
142 155

  
143
def value(node):
156
def value_node(node):
144 157
    if is_elem(node):
145 158
        iter_ = NodeIter(node)
146 159
        util.skip(iter_, is_comment)
147
        try: return iter_.next().nodeValue
160
        try: return iter_.next()
148 161
        except StopIteration: return None
149
    else: return node.nodeValue
162
    else: return node
150 163

  
164
def value(node):
165
    return util.do_ignore_none(lambda n: n.nodeValue, value_node(node))
166

  
151 167
def is_whitespace(node): return is_text_node(node) and value(node).isspace()
152 168

  
153 169
def set_value(node, value):
154
    if is_elem(node): node.appendChild(node.ownerDocument.createTextNode(value))
170
    value_node_ = value_node(node)
171
    if value != None:
172
        if value_node_ != None:
173
            value_node_.nodeValue = value
174
        else:
175
            assert is_elem(node)
176
            node.appendChild(node.ownerDocument.createTextNode(value))
155 177
    else: node.nodeValue = value
156 178

  
157 179
class NodeTextEntryIter:
......
186 208

  
187 209
def set_id(node, id_): node.setAttribute('id', id_)
188 210

  
189
##### Modifying/replacing a node
211
##### Child nodes
190 212

  
191 213
def set_child(node, name, value):
192 214
    '''Note: does not remove any existing child of the same name'''
......
194 216
    set_value(child, value)
195 217
    node.appendChild(child)
196 218

  
197
def remove(node): node.parentNode.removeChild(node)
198

  
199
def replace(old, new):
200
    '''@param new Node|None'''
201
    if new == None: old.parentNode.removeChild(old)
202
    else: old.parentNode.replaceChild(new, old) # note order reversed
203

  
204
def replace_with_text(node, new):
205
    if util.is_str(new): new = node.ownerDocument.createTextNode(new)
206
    replace(node, new)
207

  
208
##### Searching child nodes
209

  
210 219
def by_tag_name(node, name, last_only=False, ignore_namespace=False):
211 220
    '''last_only optimization returns only the last matching node'''
212 221
    if ignore_namespace: filter_name = strip_namespace

Also available in: Unified diff