zoukankan      html  css  js  c++  java
  • 节点遍历相关操作

    from lxml import etree
    
    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"
    root.append(etree.Entity("#234"))
    root.append(etree.Comment("some comment"))
    
    print('遍历所有节点,包括entity和comment')
    print(etree.tostring(root, pretty_print=True))
    print('
    ')
    
    print('遍历所有节点')
    for element in root.iter():
        print("%s - %s" % (element.tag, element.text))
    print('
    ')
    
    print('遍历所有child节点')
    for element in root.iter("child"):
        print("%s - %s" % (element.tag, element.text))
    print('
    ')
    
    print('遍历所有child节点和another节点')
    for element in root.iter("another", "child"):
        print("%s - %s" % (element.tag, element.text))
    print('
    ')
    
    print('遍历所有节点,包括entity和comment')
    for element in root.iter():
        if isinstance(element.tag, str):  # or 'str' in Python 3
            print("%s - %s" % (element.tag, element.text))
        else:
            print("SPECIAL: %s - %s" % (element, element.text))
    print('
    ')
    
    print('遍历所有节点')
    for element in root.iter(tag=etree.Element):
        print("%s - %s" % (element.tag, element.text))
    print('
    ')
    
    print('遍历所有entity')
    for element in root.iter(tag=etree.Entity):
        print(element.text)
    print('
    ')

    输出:

    遍历所有节点,包括entity和comment
    b'<root><child>Child 1</child><child>Child 2</child><another>Child 3</another>&#234;<!--some comment--></root>
    '
    
    
    遍历所有节点
    root - None
    child - Child 1
    child - Child 2
    another - Child 3
    <cyfunction Entity at 0x00000229A2BA41B8> - &#234;
    <cyfunction Comment at 0x00000229A2BA4048> - some comment
    
    
    遍历所有child节点
    child - Child 1
    child - Child 2
    
    
    遍历所有child节点和another节点
    child - Child 1
    child - Child 2
    another - Child 3
    
    
    遍历所有节点,包括entity和comment
    root - None
    child - Child 1
    child - Child 2
    another - Child 3
    SPECIAL: &#234; - &#234;
    SPECIAL: <!--some comment--> - some comment
    
    
    遍历所有节点
    root - None
    child - Child 1
    child - Child 2
    another - Child 3
    
    
    遍历所有entity
    &#234;

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  • 相关阅读:
    点击空白处回收键盘
    数据处理文件读取---预习 1.30
    界面通信之属性传值、代理传值
    UITableView的添加、删除、移动操作
    加载plist文件数据的方法
    UITableViewCell的重用机制
    自定义cell自适应高度
    关于Xcode7更新之后使用 SDWebImage 图片加载不出来
    简述frame、bounds、center
    layoutSubviews方法需要被调用的情况有哪些
  • 原文地址:https://www.cnblogs.com/shiliye/p/11759478.html
Copyright © 2011-2022 走看看