Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with xml with python Standard Library ElementTree

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report