Revision 21
Added by Aaron Marcuse-Kubitza about 13 years ago
xml_util.py | ||
---|---|---|
26 | 26 |
|
27 | 27 |
def first_elem(node): return NodeElemIter(node).next() |
28 | 28 |
|
29 |
class NodeElemReverseIter: |
|
30 |
def __init__(self, node): self.child = node.lastChild |
|
31 |
|
|
32 |
def __iter__(self): return self |
|
33 |
|
|
34 |
def curr(self): |
|
35 |
while self.child != None: |
|
36 |
if self.child.nodeType == Node.ELEMENT_NODE: return self.child |
|
37 |
self.child = self.child.previousSibling |
|
38 |
raise StopIteration |
|
39 |
|
|
40 |
def next(self): |
|
41 |
child = self.curr() |
|
42 |
self.child = self.child.previousSibling |
|
43 |
return child |
|
44 |
|
|
45 |
def last_elem(node): return NodeElemReverseIter(node).next() |
|
46 |
|
|
29 | 47 |
class NodeParentIter: |
30 | 48 |
def __init__(self, node): self.node = node |
31 | 49 |
|
... | ... | |
47 | 65 |
|
48 | 66 |
def value(node): |
49 | 67 |
if node.firstChild != None: return node.firstChild.nodeValue.strip() |
50 |
else: return None |
|
68 |
else: return node.nodeValue |
|
69 |
|
|
70 |
def by_tag_name(node, name): |
|
71 |
for child in NodeElemReverseIter(node): |
|
72 |
if child.tagName == name: return child |
|
73 |
return None |
Also available in: Unified diff
Merged data2xml XPath functionality into xpath.py. Merged data2xml xml_dom.py and xml2db xml_util.py into identical xml_util.py for each script.