【问题标题】:XML File Parsing and fetching the tags?XML 文件解析和获取标签?
【发布时间】:2013-03-09 11:01:41
【问题描述】:

xml =

<company>Mcd</company>        
<Author>Dr.D</Author> 

我要带 Mcd 和 Dr.D.
我的尝试

import xml.etree.ElementTree as et
e = et.parse(xml)
root = e.getroot()
for node in root.getiterator("company"):
   print node.tag

希望得到慷慨的帮助。

【问题讨论】:

  • 嘿得到了我的答案:) 使用 `xml.dom.minidom import parseString
  • xml.dom.minidom 不是推荐的课程,除非您已经非常了解 DOM API。该文档正确地建议您改用ElementTree

标签: python xml python-3.x xml-parsing xml.etree


【解决方案1】:

只需找到匹配的一个标签,然后取.text属性:

company = root.find('.//company').text
author = root.find('.//Author').text

【讨论】:

    【解决方案2】:

    试试这个。

       from xml.etree import ElementTree as ET
        xmlFile = ET.iterparse(open('some_file.xml','r'))
    
        for tag, value in xmlFile:
            print value.text
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多