-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
examples.py
executable file
·61 lines (49 loc) · 1.3 KB
/
examples.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
61
#!/usr/bin/env python
"""
Usage examples for untangle
"""
import untangle
def access():
"""
Shows basic attribute access and node navigation.
"""
o = untangle.parse('<node id="5">This is cdata<subnode value="abc"/></node>')
return "Node id = %s, subnode value = %s" % (o.node["id"], o.node.subnode["value"])
def siblings_list():
"""
Shows child element iteration
"""
o = untangle.parse(
"""
<root>
<child name="child1"/>
<child name="child2"/>
<child name="child3"/>
</root>
"""
)
return ",".join([child["name"] for child in o.root.child])
def access_cdata():
"""
Shows how to handle CDATA elements
"""
o = untangle.parse('<node id="5">This is cdata<subnode value="abc"/></node>')
return "%s" % (o.node.cdata)
examples = [
(
"Access children with parent.children and"
' attributes with element["attribute"]',
access,
),
("Access siblings as list", siblings_list),
("Access cdata text or other data", access_cdata),
]
if __name__ == "__main__":
for description, func in examples:
print("=" * 70)
print(description)
print("=" * 70)
print()
print(func())
print()
# vim: set expandtab ts=4 sw=4: