Python XML parser parser เป็นวิธีที่ง่ายที่สุดวิธีหนึ่งในการอ่านและดึงข้อมูลที่เป็นประโยชน์จากไฟล์ XML ในบทช่วยสอนสั้นๆ นี้ เราจะมาดูกันว่าเราสามารถแยกวิเคราะห์ไฟล์ XML แก้ไขและสร้างเอกสาร XML โดยใช้ python ElementTree XML API ได้อย่างไร
Python ElementTree API เป็นวิธีที่ง่ายที่สุดในการแยก แยกวิเคราะห์ และแปลงข้อมูล XML
มาเริ่มกันเลยโดยใช้ python XML parser โดยใช้ ElementTree:
ตัวอย่าง1
การสร้างไฟล์ XML
อันดับแรก เราจะสร้างไฟล์ XML ใหม่ที่มีองค์ประกอบและองค์ประกอบย่อย
#Import required library
import xml.etree.ElementTree as xml
def createXML(filename):
# Start with the root element
root = xml.Element("users")
children1 = xml.Element("user")
root.append(children1)
tree = xml.ElementTree(root)
with open(filename, "wb") as fh:
tree.write(fh)
if __name__ == "__main__":
createXML("testXML.xml") เมื่อเราเรียกใช้โปรแกรมข้างต้น ไฟล์ใหม่จะถูกสร้างขึ้นชื่อ “textXML.xml” ในไดเร็กทอรีการทำงานเริ่มต้นปัจจุบันของเรา:

ซึ่งมีเนื้อหาดังนี้:
<users><user /></users>
โปรดทราบว่าในขณะที่เขียนไฟล์ เราได้ใช้โหมด 'wb' เช่น เขียนไฟล์ในโหมดไบนารี
การเพิ่มค่าให้กับองค์ประกอบ XML
ให้ค่าบางอย่างแก่องค์ประกอบ XML ในโปรแกรมด้านบนของเรา:
#Import required library
import xml.etree.ElementTree as xml
def createXML(filename):
# Start with the root element
root = xml.Element("users")
children1 = xml.Element("user")
root.append(children1)
userId1 = xml.SubElement(children1, "Id")
userId1.text = "hello"
userName1 = xml.SubElement(children1, "Name")
userName1.text = "Rajesh"
tree = xml.ElementTree(root)
with open(filename, "wb") as fh:
tree.write(fh)
if __name__ == "__main__":
createXML("testXML.xml") หลังจากรันโปรแกรมข้างต้นแล้ว เราจะพบว่ามีการเพิ่มองค์ประกอบใหม่ด้วยค่าต่างๆ เช่น:
<users> <user> <Id>hello</Id> <Name>Rajesh</Name> </user> </users>
เอาต์พุตด้านบนดูโอเค
มาเริ่มแก้ไขไฟล์กัน:
การแก้ไขข้อมูล XML
มาเพิ่มข้อมูลบางส่วนจากไฟล์ในโปรแกรมที่เรามีอยู่
newdata.xml
<users> <user> <id>1a</id> <name>Rajesh</name> <salary>NA</salary> </user> <user> <id>2b</id> <name>TutorialsPoint</name> <salary>NA</salary> </user> <user> <id>3c</id> <name>Others</name> <salary>NA</salary> </user> </users>
ด้านบนนี้เป็นไฟล์ xml ปัจจุบันของเรา มาลองอัปเดตเงินเดือนของผู้ใช้แต่ละคนกัน:
#Import required library
import xml.etree.ElementTree as ET
def updateET(filename):
# Start with the root element
tree = ET.ElementTree(file=filename)
root = tree.getroot()
for salary in root.iter('salary'):
salary.text = '500000'
tree = ET.ElementTree(root)
with open("newdata.xml", "wb") as fh:
tree.write(fh)
if __name__ == "__main__":
updateET("newdata.xml") ผลลัพธ์

เราจึงเห็นว่าเงินเดือนเปลี่ยนจาก 'NA' เป็น '500000'
ตัวอย่าง:Python XML Parser
ตอนนี้เรามาเขียนโปรแกรมอื่นที่จะแยกวิเคราะห์ข้อมูล XML ที่มีอยู่ในไฟล์และพิมพ์ข้อมูล
#Import required library
import xml.etree.cElementTree as ET
def parseXML(file_name):
# Parse XML with ElementTree
tree = ET.ElementTree(file=file_name)
print(tree.getroot())
root = tree.getroot()
print("tag=%s, attrib=%s" % (root.tag, root.attrib))
# get the information via the children!
print("-" * 25)
print("Iterating using getchildren()")
print("-" * 25)
users = root.getchildren()
for user in users:
user_children = user.getchildren()
for user_child in user_children:
print("%s=%s" % (user_child.tag, user_child.text))
if __name__ == "__main__":
parseXML("newdata.xml") ผลลัพธ์
<Element 'users' at 0x0551A5A0>
tag = users, attrib = {}
-------------------------
Iterating using getchildren()
-------------------------
id = 1a
name = Rajesh
salary = 500000
id = 2b
name = TutorialsPoint
salary = 500000
id = 3c
name = Others
salary = 500000