【问题标题】:How to print an Objectified Element?如何打印对象化元素?
【发布时间】:2010-06-06 15:04:50
【问题描述】:

我有xml格式:

<channel>
    <games>
        <game slot='1'>
            <id>Bric A Bloc</id>
            <title-text>BricABloc Hoorah</title-text>
            <link>Fruit Splat</link>
        </game>
    </games>
</channel>

我已经使用 lxml.objectify 解析了这个 xml,通过:

tree = objectify.parse(file)

&lt;games&gt; 下可能会有多个 &lt;game&gt;s。 我知道我可以通过以下方式生成&lt;game&gt; 对象列表:

[ tree.games[0].game[0:4] ]

我的问题是,这些对象是什么类,是否有一个函数可以打印这些对象所属的任何类的任何对象?

【问题讨论】:

    标签: python lxml


    【解决方案1】:

    也许用

    for game in tree.games[0].game[0:4]:
        print(lxml.objectify.dump(game))
    

    产生

    game = None [ObjectifiedElement]
      * slot = '1'
        id = 'Bric A Bloc' [StringElement]
        title-text = 'BricABloc Hoorah' [StringElement]
        link = 'Fruit Splat' [StringElement]
    

    print(game) 表明每个game 都是一个lxml.ojectify.ObjectifiedElement

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-30
      • 2020-09-27
      • 2020-02-24
      • 2020-05-09
      • 2018-04-09
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      相关资源
      最近更新 更多