Python serialization function-xml details

Source: Internet
Author: User
This article mainly introduces in detail the python serialization function, which has some reference value, interested parties can refer to this article for details about the python serialization function. xml has some reference value. interested parties can refer to it.

     
          
   
    1
           
   
    1978
           27804358                    
      
          
   
    2
           
   
    1982
        8273371                    
  
 

The above is an example of xml text. to process xml text, you need to import a module.

Import xml. etree. ElementTree as ET

# Because the name of the xml module is too long, use as to give it an alias called ET.

ET. parse () directly reads xml text from the file and parses the xml text into an xml tree object.

Tree = ET. parse ("pa. xml ")

Obtain the root node of the xml tree.

Root = tree. getroot ()

Obtain the label (name) of the root node ).

Root. tag

# Traverse xml documents

For child in root:

Print (child. tag, child. attrib)

For I in child:

Print (I. tag, I. text)

Note! If you want to retrieve the child nodes in the xml text, you must use the root node to retrieve them. you can obtain the tag name in the node. text can get the content contained in each node, and attrib can get the attributes in the label of the node.

# Obtain the text content in the album_sales_volume label of each subnode.

For I in root. iter ("album_sales_volume "):

Print I. text

# If you want to obtain attributes in a tag, simply change text to attrib.

Modify:

For node in root. iter ('year '):

New_year = int (node. text) + 1

Node. text = str (new_year) # modify content

Node. set ("flop", "no") # modify tag attributes.

Tree. write ("xmltest. xml ")

Delete:

For country in root. findall ('country '):

Rank = int (country. find ('rank '). text)

If rank> 50:

Root. remove (country)

Tree. write ('output. XML ')

Root. findall () is used to find the child node with the specified name from the root node.

Root. remove () is used to delete a node.

Generate xml text.

Import xml. etree. ElementTree as ET

New_xml = ET. Element ("namelist ")

Name = ET. SubElement (new_xml, "name", attrib = {"enrolled": "yes "})

Age = ET. SubElement (name, "age", attrib = {"checked": "no "})

Sex = ET. SubElement (name, "sex ")

Sex. text = '33'

Name2 = ET. SubElement (new_xml, "name", attrib = {"enrolled": "no "})

Age = ET. SubElement (name2, "age ")

Age. text = '19'

Et = ET. ElementTree (new_xml) # generate a document object

Et. write ("test. xml", encoding = "UTF-8", xml_declaration = True)

ET. dump (new_xml) # print the generated format

The above is a detailed description of the python serialization function xml. 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.