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

Detailed explanation of python xml parsing examples

黄舟
Release: 2016-12-14 18:00:33
Original
1525 people have browsed it

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 =&#39;1&#39;>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): 
&#39;&#39;&#39;&#39;&#39;打印结点基本信息&#39;&#39;&#39; 
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 (&#39;first.xml&#39;)   
>>> res = root.findall("person") 
[<Element &#39;person&#39; at 0x00000000033388B8>, <Element &#39;person&#39; at 0x0000000003413D68>] 
   
注意:findall只查询直接的子节点 
>>> r1 = root.findall("id") 
>>> r1 
[] 
>>> r =tree.findall(".//id") 
>>> for e in r: 
  print( e,e.text) 
      
<Element &#39;id&#39; at 0x00000000034279F8> 1 
<Element &#39;id&#39; at 0x0000000003427B38> 2
Copy after login

find:

#find()方法用来返回第一个匹配到的元素。当我们认为只会有一个匹配,或者有多个匹配但我们只关心第一个的时候,这个方法是很有用的。 
>>> res[0].find("id") 
<Element &#39;id&#39; 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 &#39;NoneType&#39;> 
>>> res[0].find("id") 
<Element &#39;id&#39; 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)!


Related labels:
source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template