【问题标题】:Accessing child nodein an xml in python在python中访问xml中的子节点
【发布时间】:2011-01-23 15:27:41
【问题描述】:

如何在下面的 XML 中检索类型的值

 <info><category>Flip</category><info>2</info><type>Tree</type></info>

【问题讨论】:

    标签: python xml xml-parsing


    【解决方案1】:

    使用元素树:

    import xml.etree.ElementTree as E
    e = E.parse("test.xml")
    print(e.find("type").text)
    

    使用 minidom:

    import xml.dom.minidom
    d = xml.dom.minidom.parse("test.xml")
    print(d.getElementsByTagName("type")[0].firstChild.data)
    

    使用BeautifulSoup

    from BeautifulSoup import BeautifulStoneSoup
    soup = BeautifulStoneSoup(open("test.xml"))
    print(soup.find("type").text)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 2016-02-02
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多