-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmlelementtree.py
47 lines (35 loc) · 1.12 KB
/
xmlelementtree.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
from lxml import etree
def main():
postCount = 0
entryCount = 0
# build a doc structure using the ElementTree API
doc = etree.parse('xmldata.xml').getroot()
print(doc.tag)
# Access the value of an attribute
print(doc.attrib['title'])
# Iterate over tags
for elem in doc.findall('post'):
print(elem.tag)
# CountDecorator the number of posts
postCount = len(doc.findall('post'))
entryCount = len(doc.findall('.//entry'))
print(f'There are {postCount} post elements')
print(f'There are {entryCount} entry elements')
# Create a new post
newPost = etree.SubElement(doc, 'post')
newPost.text = 'This is a new post'
# CountDecorator the number of posts
postCount = len(doc.findall('post'))
entryCount = len(doc.findall('.//entry'))
print(f'There are now {postCount} post elements')
print(f'There are now {entryCount} entry elements')
if __name__ == '__main__':
main()
# blogposts
# Blog Posts Collection
# post
# post
# There are 2 post elements
# There are 3 entry elements
# There are now 3 post elements
# There are now 3 entry elements