Google's blog article about parsing XML files in Python does not contain XML file errors. Python file:
The code is as follows:
# Parsexml. py
# In this example, the python online document has been modified and added.
Import xml. parsers. expat
# Control print indentation
Level = 0
# Obtain a node name and a set of attribute values
Def start_element (name, attrs ):
Global level
Print ''' * level, 'Start element: ', name, attrs
Level = level + 1
# Obtain the end name of a node
Def end_element (name ):
Global level
Level = level-1
Print ''' * level, 'end element: ', name
# Obtain the intermediate value of a node
Def char_data (data ):
If (data = '\ n '):
Return
If (data. isspace ()):
Return
Global level
Print ''' * level, 'character data: ', data
P = xml. parsers. expat. ParserCreate ()
P. StartElementHandler = start_element
P. EndElementHandler = end_element
P. CharacterDataHandler = char_data
P. returns_unicode = False
F = file ('sample. XML ')
P. ParseFile (f)
F. close ()
XML file (sample. xml ):
The code is as follows:
222222222
134567890
11111111
15909878909