The xml file can be parsed and processed with the built-in xml.dom of python.
What is xml?
XML stands for Extensible Markup Language (EXtensible Markup Language)
XML is a markup language, very similar to HTML
XML is designed to transmit data, not display data
XML is designed to be self-describing.
XML is the recommended standard of W3C
xml.dom specific operation examples:
This example uses the xml module to write to the xml file
from xml.dom.minidom import Document
doc =Document()
people = doc.createElement("people")
doc.appendChild(people)
aperson = doc.createElement("person")
people.appendChild(aperson)
name = doc.createElement("name")
aperson.appendChild(name)
personname = doc.createTextNode("Annie")
name.appendChild(personname)
filename ="people.xml"
f =open(filename,"w")
f.write(doc.toprettyxml(indent=" "))
f.close()
Content expansion:
XML file parsing
There are three common methods for python to parse XML:
One is the xml.dom.* module, which is the implementation of the W3C DOM API. If you need to deal with the DOM API, this module is very suitable;
The second is the xml.sax.* module, which is the implementation of the SAX API. This module sacrifices convenience in exchange for speed and memory usage. SAX is an event-based API, which means it can handle huge quantities "on the air" The document does not need to be completely loaded into memory;
The third is the xml.etree.ElementTree module (ET for short), which provides a lightweight Python-style API. Compared to DOM, ET is much faster, and there are many pleasant APIs that can be used, compared to SAX. Said ET's ET.iterparse also provides an "in the air" processing method. There is no need to load the entire document into memory. The average performance of ET is similar to that of SAX, but the API is more efficient and easy to use.
So far, this article on how Python generates xml files is introduced. For more related Python generation methods of xml files, please search for previous articles on ZaLou.Cn or continue to browse related articles below. Hope you will support ZaLou.Cn more in the future. !
Recommended Posts