-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_helpers.py
60 lines (52 loc) · 1.98 KB
/
xml_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import xml.dom.minidom
def child_elements(node, name):
"""Iterate through direct child elements of a node that have the given tag name"""
assert isinstance(node, xml.dom.minidom.Node), "child element was %r, not node" % type(node)
for subnode in node.childNodes:
if subnode.nodeType != xml.dom.minidom.Node.ELEMENT_NODE:
continue
assert isinstance(subnode, xml.dom.minidom.Element)
if subnode.nodeName != name:
continue
yield subnode
def child_element_text(node, name, default_value=""):
"""Get the text of the direct child element(s) of a node"""
parts = []
found = False
for element in child_elements(node, name):
found = True
for node in element.childNodes:
if node.nodeType == element.TEXT_NODE:
assert isinstance(node, xml.dom.minidom.Text)
# noinspection PyUnresolvedReferences
parts.append(node.nodeValue)
if not found:
return default_value
return "".join(parts)
def element_text(element, default_value=""):
parts = []
found = False
for node in element.childNodes:
if node.nodeType == element.TEXT_NODE:
found = True
assert isinstance(node, xml.dom.minidom.Text)
# noinspection PyUnresolvedReferences
parts.append(node.nodeValue)
if not found:
return default_value
return "".join(parts)
def get_path_elements(node, path):
""":rtype: collections.Iterable[xml.dom.minidom.Element]"""
if "/" in path:
first, rest = path.split("/", 1)
if first == "..":
parent = node.parentNode
for result in get_path_elements(parent, rest):
yield result
else:
for child_node in child_elements(node, first):
for result in get_path_elements(child_node, rest):
yield result
else:
for result in child_elements(node, path):
yield result