Get the App
SLTechnology News&Howtos  ›  Development  › 

How to deal with xml with python Standard Library ElementTree

Shulou Source: shulou.com Published: 2022-05-31 16:29:20 09月22日 Update

This article introduces the relevant knowledge of "how to use python standard library ElementTree to deal with xml". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Sample usage

Refer to the official document and create a country_data.xml test document as follows:

1 2008 141100 4 2011 59900 68 2011 13600

Use the following code to read out and print the data

From xml.etree.ElementTree data = ElementTree.ElementTree (file='country_data.xml') country_list = data.findall ('country') # finds all the tag named' country' and returns a list of Element objects. For country in country_list: name = country.attrib.get ('name','') print name,'', for item in country: if item.tag = = 'neighbor': name = item.attrib.get (' name','') direction = item.attrib.get ('direction',') print'{0} ({1}) '.format (name, direction),'' Else: print item.text,'', print''

Among them

Data = ElementTree.ElementTree (file='country_data.xml')

To get an ElementTree object, you can also use the

Tree = ElementTree.parse ('country_data.xml')

The Element object has the following properties and operations elem.tag the name of the Element object (tag) elem.text document content elem.attrib attribute value dictionary elem.tail other data stored with the attribute

Elem [n] returns the nth child element of elem

Elem [n] = new_elem changes the nth child element of elem to a different element new_elem

Del elem [n] Delete child elements

Number of len (elem) child elements

Elem.find (path)

Elem.getchildren () returns all child elements in document order

Elem.items () returns the attribute values of all elements as a (name, value) pair list.

Encountered illegal format xmlExpatError: no element found

When bad.xml is an empty document, the content is as follows:

Execute the following python code and encounter a xml.parser.expat.ExpatError exception:

Import xml.etree.ElementTree as ETET.parse ('bad.xml')

Xml.parsers.expat.ExpatError: no element found: line 3, column 0

ExpatError: mismatched tag

When the corresponding closing marker is not found in bad.xml, the content is as follows:

Cannot be used as a closing tag because it is case-sensitive.

Xml.parsers.expat.ExpatError: mismatched tag: line 3, column 2

ExpatError: not well-formed (invalid token)

When the attribute value in bad.xml is not enclosed in double quotation marks ("), the following exception is encountered:

Illegal symbol in bad.xml, in'of "if salary < 1000 then" statement

Tags: Elements content attributes documents objects standards processing size code data more tags knowledge different practical successful learning next middle name dilemma Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Information Shulou Tech Info vpn macOS