If we want to use the Python programming language for XML-related operations, how can we meet this requirement well? Here we will learn about the method of parsing XML in Python, and I believe it can help you fully master the application technology in this area.
Python parsing XML code example:
- <Text>
- <Font size = "86" spacing = "17"/>
- Rate improvement without the Tang Yuan's method, which has been written in the western regions for seven years
- </Text>
- <Text>
- Rate improvement without the Tang Yuan's method, which has been written in the western regions for seven years
- </Text>
- Import xml. etree. ElementTree
- Tree = xml. etree. ElementTree. ElementTree (file = "e: \ Python \ 1.xml ")
- Elem = tree. getroot ()
- File = open ("e: \ a.txt", 'w ')
- For a in elem. getiterator ("text "):
- # Print len (a. _ children)
- If len (a. _ children)> 0:
- For node in:
- If node. tail <> None:
- Node. tail. encode ('utf-8 ')
- File. write (node. tail. encode ('utf-8 '))
- # Print xml. etree. ElementTree. iselement (node)
- Else:
- If a. text <> None:
- File. write (a. text. encode ('utf-8 '))
The above is our introduction to the methods for parsing XML in Python.