Python common built-in modules-xml module (detailed description), pythonxml

Source: Internet
Author: User

Python common built-in modules-xml module (detailed description), pythonxml

Xml can be used to tag data and define data types. It is a source language that allows you to define your own markup language. The structure is similar to HTML hypertext markup language. However, they are designed for different purposes. hypertext markup language is designed to display data, and its focus is on the appearance of the data. It is designed to transmit and store data, with the focus on data content. So how does Python process XML files? Let's take a look at the xml module of common Python built-in modules.

ElementTree is the XML processing module of python. It provides a lightweight object model. When using the ElementTree module, You need to import xml. etree. ElementTree. ElementTree indicates the entire XML node tree, while Element indicates a separate node in the number of nodes.

Construct an XML file

ElementTree (tag), where tag indicates the root node and initializes an ElementTree object.

The Element (tag, attrib = {}, ** extra) function is used to construct a root node of XML. tag indicates the name of the root node, and attrib is an optional function, attribute of a node.

SubElement (parent, tag, attrib = {}, ** extra) is used to construct a subnode Element of an existing node. text and SubElement. text indicates the additional content attribute of the element object, Element. tag and Element. attrib indicates the tag and attribute of the element object.

ElementTree. write (file, encoding = 'US-ascii ', xml_declaration = None, default_namespace = None, method = 'xml'), the function creates an xml file, write the number of nodes to the XML file.

The following code uses the sitemap. xml file of a new website as an example.

#! /Usr/bin/env python #-*-coding: UTF-8-*-from xml. etree import ElementTree as ETdef build_sitemap (): urlset = ET. element ("urlset") # Set a root node with the label urlset url = ET. subElement (urlset, "url") # create a subnode loc = ET under the root node urlset. subElement (url, "loc") loc. text = "http: // www/baidu.com" lastmod = ET. subElement (url, "lastmod") lastmod. text = "2017-10-10" changefreq = ET. subElement (url, "changefreq") changefreq. text = "daily" priority = ET. subElement (url, "priority") priority. text = "1.0" tree = ET. elementTree (urlset) tree. write ("sitemap. xml ") if _ name _ = '_ main _': build_sitemap ()

The result is shown in:

Parse and modify XML files

ElementTree. parse (source, parser = None ),Load the xml file and return the ElementTree object. Parser is an optional parameter. If it is null, the standard XMLParser parser is used by default.

ElementTree. getroot (),Get the root node. Returns the element Object of the root node.

Element. remove (tag ),Delete the following functions for sub-nodes named tag under root. All objects of ElementTree and Element contain.

Find (match ),Obtain the first child node that matches the match. match can be a tag name or path. Returns an element findtext (match, default = None). The findall (match) content of the first configured match element is obtained, match can be a tag or path. It will return a list containing the matching elements information iter (tag) and create an iterator with the current node as the root node.

Take sitemap. xml created above as an example to modify it. The code example is as follows:

#!/usr/bin/env python# -*- coding:utf-8 -*- from xml.etree import ElementTree as ETtree = ET.parse("sitemap.xml")url = tree.find("url")for rank in tree.iter('loc'):  rank.text = "http://www.adminba.com"tree.write("sitemap.xml")

The code above changes the url to a http://www.adminba.com. In addition, the node also has the set (set node attributes) and attrib (delete node attributes) methods.

This xml module (detailed description), commonly used built-in modules in Python, is all the content shared by the editor. I hope you can give us a reference and support for the help house.

Related Article

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.