Lxml. etree tutorial 6: Tree Iteration

Source: Internet
Author: User

Elements provide a tree iterator for this purpose. It yields elements in document order, I. e. in the order their tags wowould appear if you serialised the tree to XML:

 
>>> Root = etree. element ("root") >>> etree. subelement (root, "child "). TEXT = "CHILD 1" >>> etree. subelement (root, "child "). TEXT = "Child 2" >>> etree. subelement (root, "another "). TEXT = "child 3" >>> print (etree. tostring (root, pretty_print = true )) <root> <child> Child 1 </child> <child> Child 2 </child> <another> child 3 </Another> </root> for element in root. ITER ():... print ("% s-% s" % (element. tag, element. text) Root-nonechild-Child 1 child-Child 2 Another-child 3

If you know you are only interested in a single tag, you can pass its name to ITER () to have it filter for you. starting with lxml 3.0, you can also pass more than one tag to intercept on multiple tags during iteration.

 
>>> For element in root. ITER ("child "):... print ("% s-% s" % (element. tag, element. text) Child-Child 1 child-Child 2 >>> for element in root. ITER ("another", "child "):... print ("% s-% s" % (element. tag, element. text) Child-Child 1 child-Child 2 Another-child 3

By default, iteration yields all nodes in the tree, including processinginstructions, comments and entity instances. if you want to make sure only element objects are returned, you can pass the element factory as tag parameter:

>>> Root. append (etree. entity ("#234") >>> root. append (etree. comment ("some comment") >>> for element in root. ITER ():... if isinstance (element. tag, basestring ):... print ("% s-% s" % (element. tag, element. text ))... else :... print ("Special: % s-% s" % (element, element. text) Root-nonechild-Child 1 child-Child 2 Another-child 3 Special: Your-specific special: <! -- Some comment -->-some comment >>> for element in root. ITER (TAG = etree. element ):... print ("% s-% s" % (element. tag, element. text) Root-nonechild-Child 1 child-Child 2 Another-child 3 >>> for element in root. ITER (TAG = etree. entity ):... print (element. text) plaintext

Note that passing a wildcard "*" tag name will also yield all element nodes (and only elements ).

In lxml. etree, elements provide further iterators for all directions ctions in the tree: children, parents (or rather ancestors) and siblings.

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.