Project

General

Profile

1
# XML DOM tree manipulation
2

    
3
from xml.dom import Node
4

    
5
class NodeElemIter:
6
    def __init__(self, node): self.child = node.firstChild
7
    
8
    def __iter__(self): return self
9
    
10
    def curr(self):
11
        while self.child != None:
12
            if self.child.nodeType == Node.ELEMENT_NODE: return self.child
13
            self.child = self.child.nextSibling
14
        raise StopIteration
15
    
16
    def next(self):
17
        child = self.curr()
18
        self.child = self.child.nextSibling
19
        return child
20

    
21
def first_elem(node): return NodeElemIter(node).next()
22

    
23
def is_text(node):
24
    for child in NodeElemIter(node): return False # has an element node
25
    return True
26

    
27
def value(node):
28
    if node.firstChild != None: return node.firstChild.nodeValue
29
    else: return None
30

    
31
def get_id(node): return node.getAttribute('id')
32

    
33
def set_id(node, get_id): node.setAttribute('id', get_id)
(9-9/10)