How does Python generate xml files

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

How does Python generate xml files
How Python parses XML
How does Python store data to json files
How does Python output integers
How does python output backslashes
How does python update packages
How does python perform matrix operations
How does python change the environment
How does python call java classes
How does python import dependency packages
How does python enter interactive mode
How does python determine prime numbers
How to delete cache files in python
How does python improve the calculation speed
How to save text files in python
How does python call its own function
How does Python handle the json module
How does python distinguish return and yield
Python renames files
How does Python call cmd using OS modules
How to delete files and directories in python
How to read and write files with Python
How does python get input examples from the keyboard
How does python handle the program cannot be opened
How does python judge the module installation is complete
Python analysis of wav files
How Python converts string case
Python parses simple XML data
How to learn python quickly
How to uninstall python plugin
Does Python support multiple inheritance?
How Python implements FTP function
python batch run py files
3 ways to encrypt Python files
How to understand python objects
Explain how python references package package