Python parsing XML file instance (Figure)

Source: Internet
Author: User
The following uses the xml. etree. ElementTree module to parse XML files. The ElementTree module provides two classes for this purpose: Use the xml. etree. ElementTree module to parse XML files. The ElementTree module provides two classes for this purpose:

  • ElementTree indicates the entire XML file (a tree structure)

  • Element indicates an Element (node) in the tree)

Perform the following operations on the XML file:Migapp. xml

We can import the ElementTree module as follows: import xml. etree. ElementTree as ET

Alternatively, you can import only the parse parser: from xml. etree. ElementTree import parse.

First, open an xml file. the local file uses the open function. if it is an Internet file, use urlopen:

F = open ('migapp. XML', 'RT ', encoding = utf-8 ')

Then parse the XML.

1. parse XML files

1.1Parse root element

tree = ET.parse(f)root = tree.getroot()print('root.tag =', root.tag)print('root.attrib =', root.attrib)

1.2Parse Root's son

For child in root: # only the root son can be parsed, and the root descendant print (child. tag) print (child. attrib) cannot be parsed # attrib is a dict

1.3Root descendant parsing through index

print(root[1][1].tag)print(root[1][1].text)

1.4Resolves all specified elements by iteration

for element in root.iter('environment'):    print(element.attrib)

1.5Several useful methods

# Element. findall () parses all the sons of the specified element # element. find () parses the first son of the specified element # element. get () parses the attribute attribfor environment in root of the specified element. findall ('enable'): first_variable = environment. find ('variable') print (first_variable.get ('name '))

2. modify the XML file

Assume that we need to add an attribute size = "50" to each text element, change its text to "Benxin Tuzi", and add a child element date = "2016/01/16"

for text in root.iter('text'):    text.set('size', '50')    text.text = 'Benxin Tuzi'    text.append(ET.Element('date', attrib={}, text='2016/01/16'))tree.write('output.xml')

Migapp. xmlSection:

3 Description
  • Do not use xml. py as the file name; otherwise, the following error occurs:

ImportError: No module named 'XML. etree '; 'xml' is not a package

Analysis:

This is because we will first search for the file in the current path during import. at this time, we find that the xml. py module exists, and the xml. py file we write ourselves is not a package of course.

Note:

Delete xml. py still cannot be explained, because xml is also generated in the current path. pyc, and the priority of this file is higher than xml. py, so the interpreter still takes precedence over xml. in pyc, the file must also be deleted to solve the problem.

Conclusion:

The file name should not be the same as the package name or module name. even if you do not use this module or package in the script, a strange error may occur.

  • Many parsing functions provided by the ElementTree module need to read the entire XML file into the memory in advance, which is not a good thing for large XML parsing, especially when we read XML from networks and pipelines, non-blocking parsing is very important. In this case, we can use the XMLPullParse class in the ElementTree module for processing. Of course, we can also choose iterparse () of the ElementTree module instead. this method does not need to be fully read into the memory when parsing large XML files.

The above is the details of the python parsing XML file instance (figure). For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.