Home Backend Development Python Tutorial Detailed explanation of python xml parsing examples

Detailed explanation of python xml parsing examples

Dec 14, 2016 pm 06:00 PM
xml parsing

python xml parsing

first.xml

<info> 
<person > 
<id>1</id> 
<name>fsy</name> 
<age >24</age> 
</person> 
<person> 
<id>2</id> 
<name>jianjian</name> 
<age>24</age> 
</person> 
<count id ='1'>1000</count> 
</info>
Copy after login

from xml.etree import ElementTree as etree

Read in

def read_xml(file): 
# parse()函数会返回一个能代表整篇文档的对象。这不是根元素。要获得根元素的引用可以调用getroot()方法。 
tree = etree.parse(file) 
root = tree.getroot() 
return root
Copy after login

Get information

def print_node(node): 
'''''打印结点基本信息''' 
print("node.tag:%s" % node.tag) 
print("node.attrib:%s"%node.attrib) 
print( "node.text:%s" % node.text)
Copy after login

Search:

find_all 
>>> root = read_xml ('first.xml')   
>>> res = root.findall("person") 
[<Element 'person' at 0x00000000033388B8>, <Element 'person' at 0x0000000003413D68>] 
   
注意:findall只查询直接的子节点 
>>> r1 = root.findall("id") 
>>> r1 
[] 
>>> r =tree.findall(".//id") 
>>> for e in r: 
  print( e,e.text) 
      
<Element 'id' at 0x00000000034279F8> 1 
<Element 'id' at 0x0000000003427B38> 2
Copy after login

find:

#find()方法用来返回第一个匹配到的元素。当我们认为只会有一个匹配,或者有多个匹配但我们只关心第一个的时候,这个方法是很有用的。 
>>> res[0].find("id") 
<Element 'id' at 0x0000000003413CC8> 
>>> print_node(res[0].find("id")) 
node.tag:id 
node.attrib:{} 
node.text:1
Copy after login

find find Failure:

When using find, please note that in a Boolean context, if the ElementTree element object does not contain child elements, its value will be considered False (that is, if len(element) is equal to 0). This means if element.find('...') is not testing whether the find() method found a match; this statement is testing whether the matched element contains child elements. If you want to test whether the find() method returns an element, you need to use if element.find('...') is not None.

>>> bk = res[0].find("no") 
>>> bk 
>>> type(bk) 
<class 'NoneType'> 
>>> res[0].find("id") 
<Element 'id' at 0x0000000003413CC8> 
>>> if res[0].find("id"): 
    print("find") 
  else: 
    print("not find") 
not find 
>>> if res[0].find("id") is not None: 
    print("find") 
  else: 
    print("not find") 
find
Copy after login

The above is a detailed explanation of python xml parsing examples. Thank you for reading. I hope it can help everyone. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use Gin framework to implement XML and JSON data parsing functions Use Gin framework to implement XML and JSON data parsing functions Jun 22, 2023 pm 03:14 PM

Use Gin framework to implement XML and JSON data parsing functions

Java Error: XML Parsing Error, How to Fix and Avoid Java Error: XML Parsing Error, How to Fix and Avoid Jun 24, 2023 pm 05:46 PM

Java Error: XML Parsing Error, How to Fix and Avoid

How to solve the problem of high memory usage of XML parsing in Java development How to solve the problem of high memory usage of XML parsing in Java development Jun 29, 2023 am 09:37 AM

How to solve the problem of high memory usage of XML parsing in Java development

How to use PHP to parse XML and obtain node content How to use PHP to parse XML and obtain node content Jun 13, 2023 pm 04:31 PM

How to use PHP to parse XML and obtain node content

Solution to solve Java XML parsing exception (XMLParsingException) Solution to solve Java XML parsing exception (XMLParsingException) Aug 19, 2023 pm 01:43 PM

Solution to solve Java XML parsing exception (XMLParsingException)

A guide to XML parsing and generation in PHP A guide to XML parsing and generation in PHP Jun 11, 2023 am 11:01 AM

A guide to XML parsing and generation in PHP

PHP8.1 update: enhanced XML parsing capabilities PHP8.1 update: enhanced XML parsing capabilities Jul 07, 2023 am 09:22 AM

PHP8.1 update: enhanced XML parsing capabilities

How to reduce XML parsing memory usage in Java development How to reduce XML parsing memory usage in Java development Jun 30, 2023 pm 09:19 PM

How to reduce XML parsing memory usage in Java development

See all articles