-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdom.py
57 lines (40 loc) · 1.38 KB
/
dom.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
import requests
import xml.dom.minidom
url = "http://httpbin.org/xml"
response = requests.get(url)
domtree = xml.dom.minidom.parseString(response.text)
rootnode = domtree.documentElement
#get the name of the root node and titles
print("The root element is {}".format(rootnode))
print("Title: {0}".format(rootnode.getAttribute("title")))
items = domtree.getElementsByTagName("item")
print("There are {0} item tags".format(items.length))
#data manipulation
#create a new item tag
newItem = domtree.createElement("item")
#add some text to the item
newItem.appendChild(domtree.createTextNode("This is some text from gbenga"))
#add the item to the first slide
firstSlide = domtree.getElementsByTagName("slide")[0]
firstSlide.appendChild(newItem)
#count again
items = domtree.getElementsByTagName("item")
print("There are {0} item tags".format(items.length))
from lxml import etree
url = "http://httpbin.org/xml"
result = requests.get(url)
doc = etree.fromstring(result.content)
#access attribute
print(doc.tag)
print(doc.attrb['title'])
#iterage over tags
for elem in doc.findall("slide"):
print(elem.tag)
#create a new slide
newSlide = etree.Element(doc, "slide")
newSlide.text = "This is a new slide"
#count elements
slideCount = len(doc.findall("slide"))
itemCount = len(doc.findall(".//item"))
print("There are {0} slide tags".format(slideCount))
print("There are {0} item tags".format(itemCount))