zoukankan      html  css  js  c++  java
  • E-factory

    E-factory为生成XML和HTML提供了一种简单而紧凑的语法

    # coding:utf-8
    from lxml.builder import E
    
    def CLASS(*args): # class is a reserved word in Python
        return {"class":' '.join(args)}
    
    html = page = (
        E.html(       # create an Element called "html"
            E.head(
                E.title("This is a sample document")
            ),
            E.body(
                E.h1("Hello!", CLASS("title")),
                E.p("This is a paragraph with ", E.b("bold"), " text in it!"),
                E.p("This is another paragraph, with a", "
          ",
                E.a("link", href="http://www.python.org"), "."),
                E.p("Here are some reserved characters: <spam&egg>."),
                etree.XML("<p>And finally an embedded XHTML fragment.</p>"),
            )
        )
    )
    
    print(etree.tostring(page, pretty_print=True))
     
    '''
    输出:
    <html>
      <head>
        <title>This is a sample document</title>
      </head>
      <body>
        <h1 class="title">Hello!</h1>
        <p>This is a paragraph with <b>bold</b> text in it!</p>
        <p>This is another paragraph, with a
          <a href="http://www.python.org">link</a>.</p>
        <p>Here are some reserved characters: &lt;spam&amp;egg&gt;.</p>
        <p>And finally an embedded XHTML fragment.</p>
      </body>
    </html>
    '''

    基于属性访问的元素创建使为XML语言构建简单词汇表变得容易

    from lxml.builder import ElementMaker # lxml only !
    
    E = ElementMaker(namespace="http://my.de/fault/namespace",
                     nsmap={'p' : "http://my.de/fault/namespace"})
    
    DOC = E.doc
    TITLE = E.title
    SECTION = E.section
    PAR = E.par
    
    my_doc = DOC(
        TITLE("The dog and the hog"),
        SECTION(
            TITLE("The dog"),
            PAR("Once upon a time, "),
            PAR("And then ")
        ),
        SECTION(
            TITLE("The hog"),
            PAR("Sooner or later ")
        )
    )
    
    print(etree.tostring(my_doc, pretty_print=True))
    
    '''
    输出:
    <p:doc xmlns:p="http://my.de/fault/namespace">
      <p:title>The dog and the hog</p:title>
      <p:section>
        <p:title>The dog</p:title>
        <p:par>Once upon a time, </p:par>
        <p:par>And then </p:par>
      </p:section>
      <p:section>
        <p:title>The hog</p:title>
        <p:par>Sooner or later </p:par>
      </p:section>
    </p:doc>
    '''

    其中一个例子是模块lxml.html.builder,它为html提供了词汇表
    在处理多个命名空间时,最好为每个命名空间URI定义一个ElementMaker
    同样,请注意上面的示例如何在命名常量中预定义标记生成器
    这使得将名称空间的所有标记声明放在一个Python模块中以及从那里导入/使用标记名常量变得非常容易
    这样可以避免诸如输入错误或意外丢失名称空间之类的陷阱

  • 相关阅读:
    ios开发中超简单抽屉效果(MMDrawerController)的实现
    mysql的查询、子查询及连接查询
    iOS在线音乐播放SZKAVPlayer(基于AVPlayer的封装)
    iOS js oc相互调用(JavaScriptCore)(二)
    Linux下面如何安装Django
    国内外测绘遥感领域核心期刊
    MFC程序消息处理的顺序
    VC++中关于控件重绘函数/消息 OnPaint,OnDraw,OnDrawItem,DrawItem的区别
    C#中控件Control的Paint事件和OnPaint虚函数的区别
    Lambda表达式学习(2)
  • 原文地址:https://www.cnblogs.com/shiliye/p/11850461.html
Copyright © 2011-2022 走看看