Project

General

Profile

« Previous | Next » 

Revision 13

Initial version of xml2db. Doesn't yet handle all duplicate rows correctly.

View differences:

xml_util.py
2 2

  
3 3
from xml.dom import Node
4 4

  
5
def name_of(node): return node.tagName.lower()
6

  
7
def get_id(node): return node.getAttribute('id')
8

  
9
def set_id(node, id_): node.setAttribute('id', id_)
10

  
5 11
class NodeElemIter:
6 12
    def __init__(self, node): self.child = node.firstChild
7 13
    
......
20 26

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

  
29
class NodeParentIter:
30
    def __init__(self, node): self.node = node
31
    
32
    def __iter__(self): return self
33
    
34
    def curr(self):
35
        if self.node != None and self.node.nodeType == Node.ELEMENT_NODE:
36
            return self.node
37
        raise StopIteration
38
    
39
    def next(self):
40
        node = self.curr()
41
        self.node = self.node.parentNode
42
        return node
43

  
23 44
def is_text(node):
24 45
    for child in NodeElemIter(node): return False # has an element node
25 46
    return True
......
27 48
def value(node):
28 49
    if node.firstChild != None: return node.firstChild.nodeValue
29 50
    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)

Also available in: Unified diff