PYTHON Operation XML
Read XML files
Introduction to XML
<data> and </data> are the beginning and end of a pair of tags
<property … /> is also a correct label, ending with />, which is the abbreviation when the label has no nested content
name="cat", name is an attribute of the <data> tag, cat is the value of the name attribute
description here …is the content of the <data> tag, here is a piece of text. Of course it can also be a nesting of xml
<data name="cat" num="10"> description here ... </data> <property value="node" /> <country name="china"> <province name="beijing"> <school name="the sunshine school" /> </province> </country>
Prepare a file
<data> <teacher name="Albert"> <birthday>1980</birthday> <gender>male</gender> <subject>Math</subject> </teacher> <student name="Becky"> <birthday>2000</birthday> <gender>female</gender> <hobbies> <hobby>skating</hobby> <hobby>rocks</hobby> </hobbies> <exam absence="no"> <math>90</math> <english>90</english> <music>95</music> </exam> </student> <student name="Cindy"> <birthday>2001</birthday> <gender>female</gender> <hobbies> <hobby>reading</hobby> <hobby>guitar</hobby> </hobbies> <exam absence="yes"> </exam> </student> <student name="Duke"> <birthday>2000</birthday> <gender>male</gender> <hobbies> <hobby>football</hobby> <hobby>surfing</hobby> </hobbies> <exam absence="no"> <math>100</math> <english>80</english> <music>92</music> </exam> </student> </data>
Read the content of the xml file
# Read the .xml file tree = ("") root = () print(root)
result
<Element 'data' at 0x102d80cf8>
Iterate through XML elements
for … in … can traverse all direct child nodes of the current element
for n in root: # items() returns all <key, value> pairs of the tag print(n, , , ())
result
(<Element 'teacher' at 0x1048b9e48>, 'teacher', {'name': 'Albert'}, [('name', 'Albert')])
(<Element 'student' at 0x1048bf0f0>, 'student', {'name': 'Becky'}, [('name', 'Becky')])
(<Element 'student' at 0x1048bf3c8>, 'student', {'name': 'Cindy'}, [('name', 'Cindy')])
(<Element 'student' at 0x1048bf5f8>, 'student', {'name': 'Duke'}, [('name', 'Duke')])
Want to iterate through all children (including descendants) of the current element
for n in (): print(n, )
result
(<Element 'data' at 0x1052f0cf8>, 'data')
(<Element 'teacher' at 0x1052f0e48>, 'teacher')
(<Element 'birthday' at 0x1052f0d30>, 'birthday')
(<Element 'gender' at 0x1052f6080>, 'gender')
(<Element 'subject' at 0x1052f60b8>, 'subject')
(<Element 'student' at 0x1052f60f0>, 'student')
(<Element 'birthday' at 0x1052f6048>, 'birthday')
(<Element 'gender' at 0x1052f6128>, 'gender')
(<Element 'hobbies' at 0x1052f6198>, 'hobbies')
(<Element 'hobby' at 0x1052f6208>, 'hobby')
(<Element 'hobby' at 0x1052f6240>, 'hobby')
(<Element 'exam' at 0x1052f62b0>, 'exam')
(<Element 'math' at 0x1052f6320>, 'math')
(<Element 'english' at 0x1052f6390>, 'english')
(<Element 'music' at 0x1052f6400>, 'music')
(<Element 'student' at 0x1052f63c8>, 'student')
(<Element 'birthday' at 0x1052f6438>, 'birthday')
(<Element 'gender' at 0x1052f6470>, 'gender')
(<Element 'hobbies' at 0x1052f64a8>, 'hobbies')
(<Element 'hobby' at 0x1052f6518>, 'hobby')
(<Element 'hobby' at 0x1052f6588>, 'hobby')
(<Element 'exam' at 0x1052f65c0>, 'exam')
(<Element 'student' at 0x1052f65f8>, 'student')
(<Element 'birthday' at 0x1052f6630>, 'birthday')
(<Element 'gender' at 0x1052f6668>, 'gender')
(<Element 'hobbies' at 0x1052f66a0>, 'hobbies')
(<Element 'hobby' at 0x1052f6710>, 'hobby')
(<Element 'hobby' at 0x1052f6780>, 'hobby')
(<Element 'exam' at 0x1052f67b8>, 'exam')
(<Element 'math' at 0x1052f6828>, 'math')
(<Element 'english' at 0x1052f6898>, 'english')
(<Element 'music' at 0x1052f6908>, 'music')
Want to selectively iterate over direct child nodes
for n in ('teacher'): print(n, )
(<Element 'teacher' at 0x100f29e48>, 'teacher')
Find XML elements
Find and findall to find xml elements
# find the first element print(('student')) # find all elements print(('student'))
<Element 'student' at 0x1034300f0> [<Element 'student' at 0x1034300f0>, <Element 'student' at 0x1034303c8>, <Element 'student' at 0x1034305f8>]
demo
for n in root: if == 'student' and ('name') == 'Becky': exam_node = ('exam') for subject in exam_node: print( + " " + )
result
math 90
english 90
music 95
Add XML elements
p = (tag_name)
demo
for n in root: if == 'student' and ('name') == 'Cindy': exam_node = ('exam') exam_node.set("absence", "no") for subject in ['math', 'music']: p = (subject) = '90' exam_node.append(p) if (''): ('') ('', encoding='utf-8', xml_declaration=True)
result
<student name="Cindy">
<birthday>2001</birthday>
<gender>female</gender>
<hobbies>
<hobby>reading</hobby>
<hobby>guitar</hobby>
</hobbies>
<exam absence="no">
<math>90</math><music>90</music></exam>
</student>
Modify XML elements
demo
for n in root: if == 'student' and ('name') == 'Cindy': exam_node = ('exam') exam_node.set("absence", "no") exam_node.set("date", "2022-11-11") for subject in ['math', 'music']: p = (subject) = '90' exam_node.append(p) hobbies_node = ('hobbies').findall("hobby") hobbies_node[0].text = 'piano' p = ("hobby") ("old_hobby", 'yes') = 'reading' ('hobbies').remove(hobbies_node[1]) ('hobbies').append(p)
result
<student name="Cindy">
<birthday>2001</birthday>
<gender>female</gender>
<hobbies>
<hobby>piano</hobby>
<hobby old_hobby="yes">reading</hobby></hobbies>
<exam absence="no" date="2022-11-11">
<math>90</math><music>90</music></exam>
</student>
This is the article about Python's addition, deletion, modification and search operations on XML files. For more related contents of Python XML, please search for my previous articles or continue to browse the related articles below. I hope everyone will support me in the future!