Python xml parsing instance details, pythonxml parsing instance

Source: Internet
Author: User

Python xml parsing instance details, pythonxml parsing instance

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> 

From xml. etree import ElementTree as etree

Read

Def read_xml (file): # The parse () function returns an object that represents the entire document. This is not the root element. To obtain references to the root element, call the getroot () method. Tree = etree. parse (file) root = tree. getroot () return root

Get information

Def print_node (node): ''' print basic node information ''' print ("node. tag: % s "% node. tag) print ("node. attrib: % s "% node. attrib) print ("node. text: % s "% node. text)

Search:

Find_all >>> root = read_xml ('first. xml ') >>> res = root. findall ("person") [<Element 'person 'at 0x00000000033388B8>, <Element 'person' at 0x0000000003413D68>] Note: findall only queries direct subnodes> 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

Find:

# The find () method is used to return the first matched element. This method is useful when we think there will be only one match or multiple matches but we only care about the first one. >>> Res [0]. find ("id") <Element 'id' at 0x0000000003413CC8 >>> print_node (res [0]. find ("id") node. tag: id node. attrib: {} node. text: 1

Find failed:

When using find, note that in a Boolean context, if the ElementTree element object does not contain child elements, its value is considered False (that is, if len (element) is equal to 0 ). This means that if element. find ('... ') is not used to test whether a matching item is found in the find () method; this statement is used to test whether the matched element contains a child element. To test whether the find () method returns an element, 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 

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.