如何在Python中解析XML?

我在包含xml的数据库中有很多行,并且我正在尝试编写一个Python脚本,该脚本将遍历这些行并计算出现特定节点属性的实例数量。 例如,我的树看起来像:

<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>

如何使用Python访问XML中的属性1和2?


#1楼

这是使用cElementTree的非常简单但有效的代码。

try:
    import cElementTree as ET
except ImportError:
  try:
    # Python 2.5 need to import a different module
    import xml.etree.cElementTree as ET
  except ImportError:
    exit_err("Failed to import cElementTree from any known place")      

def find_in_tree(tree, node):
    found = tree.find(node)
    if found == None:
        print "No %s in file" % node
        found = []
    return found  

# Parse a xml file (specify the path)
def_file = "xml_file_name.xml"
try:
    dom = ET.parse(open(def_file, "r"))
    root = dom.getroot()
except:
    exit_err("Unable to open and parse input definition file: " + def_file)

# Parse to find the child nodes list of node 'myNode'
fwdefs = find_in_tree(root,"myNode")

资源:

http://www.snip2code.com/Snippet/991/python-xml-parse?fromPage=1


#2楼

您可以使用BeautifulSoup

from bs4 import BeautifulSoup

x="""<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>"""

y=BeautifulSoup(x)
>>> y.foo.bar.type["foobar"]
u'1'

>>> y.foo.bar.findAll("type")
[<type foobar="1"></type>, <type foobar="2"></type>]

>>> y.foo.bar.findAll("type")[0]["foobar"]
u'1'
>>> y.foo.bar.findAll("type")[1]["foobar"]
u'2'

#3楼

我建议ElementTree 。 相同的API还有其他兼容的实现,例如Python标准库本身中的lxmlcElementTree 。 但是,在这种情况下,他们主要添加的是更高的速度-编程的难易程度取决于ElementTree定义的API。

首先从XML构建一个Element实例root ,例如,使用XML函数,或者通过解析文件,例如:

import xml.etree.ElementTree as ET
root = ET.parse('thefile.xml').getroot()

ElementTree显示的许多其他方式中的任何一种。 然后执行以下操作:

for type_tag in root.findall('bar/type'):
    value = type_tag.get('foobar')
    print(value)

和类似的,通常很简单的代码模式。


#4楼

Python具有与expat xml解析器的接口。

xml.parsers.expat

这是一个非验证解析器,因此不会发现错误的xml。 但是,如果您知道文件是正确的,那么这很好,您可能会获得所需的确切信息,并且可以随时丢弃其余信息。

stringofxml = """<foo>
    <bar>
        <type arg="value" />
        <type arg="value" />
        <type arg="value" />
    </bar>
    <bar>
        <type arg="value" />
    </bar>
</foo>"""
count = 0
def start(name, attr):
    global count
    if name == 'type':
        count += 1

p = expat.ParserCreate()
p.StartElementHandler = start
p.Parse(stringofxml)

print count # prints 4

#5楼

我发现Python xml.domxml.dom.minidom非常简单。 请记住,DOM不适用于大量XML,但是如果您的输入很小,则可以正常工作。


#6楼

minidom是最快,最简单的方法:

XML:

<data>
    <items>
        <item name="item1"></item>
        <item name="item2"></item>
        <item name="item3"></item>
        <item name="item4"></item>
    </items>
</data>

蟒蛇:

from xml.dom import minidom
xmldoc = minidom.parse('items.xml')
itemlist = xmldoc.getElementsByTagName('item')
print(len(itemlist))
print(itemlist[0].attributes['name'].value)
for s in itemlist:
    print(s.attributes['name'].value)

输出值

4
item1
item1
item2
item3
item4

#7楼

lxml.objectify非常简单。

以您的示例文本:

from lxml import objectify
from collections import defaultdict

count = defaultdict(int)

root = objectify.fromstring(text)

for item in root.bar.type:
    count[item.attrib.get("foobar")] += 1

print dict(count)

输出:

{'1': 1, '2': 1}

#8楼

有很多选择。 如果速度和内存使用成为问题,则cElementTree看起来很棒。 与仅使用readlines读取文件相比,它的开销很小。

可以从cElementTree网站复制的下表中找到相关指标:

library                         time    space
xml.dom.minidom (Python 2.1)    6.3 s   80000K
gnosis.objectify                2.0 s   22000k
xml.dom.minidom (Python 2.4)    1.4 s   53000k
ElementTree 1.2                 1.6 s   14500k  
ElementTree 1.2.4/1.3           1.1 s   14500k  
cDomlette (C extension)         0.540 s 20500k
PyRXPU (C extension)            0.175 s 10850k
libxml2 (C extension)           0.098 s 16000k
readlines (read as utf-8)       0.093 s 8850k
cElementTree (C extension)  --> 0.047 s 4900K <--
readlines (read as ascii)       0.032 s 5050k   

正如@jfs指出的那样cElementTree与Python捆绑在一起:

  • Python 2: from xml.etree import cElementTree as ElementTree
  • Python 3: from xml.etree import ElementTree (自动使用加速的C版本)。

#9楼

为了简单起见,我建议使用xmltodict

它将您的xml解析为OrderedDict;

>>> e = '<foo>
             <bar>
                 <type foobar="1"/>
                 <type foobar="2"/>
             </bar>
        </foo> '

>>> import xmltodict
>>> result = xmltodict.parse(e)
>>> result

OrderedDict([(u'foo', OrderedDict([(u'bar', OrderedDict([(u'type', [OrderedDict([(u'@foobar', u'1')]), OrderedDict([(u'@foobar', u'2')])])]))]))])

>>> result['foo']

OrderedDict([(u'bar', OrderedDict([(u'type', [OrderedDict([(u'@foobar', u'1')]), OrderedDict([(u'@foobar', u'2')])])]))])

>>> result['foo']['bar']

OrderedDict([(u'type', [OrderedDict([(u'@foobar', u'1')]), OrderedDict([(u'@foobar', u'2')])])])

#10楼

import xml.etree.ElementTree as ET
data = '''<foo>
           <bar>
               <type foobar="1"/>
               <type foobar="2"/>
          </bar>
       </foo>'''
tree = ET.fromstring(data)
lst = tree.findall('bar/type')
for item in lst:
    print item.get('foobar')

这将打印foobar属性的值。


#11楼

为了增加另一种可能性,您可以使用untangle ,因为它是一个简单的xml-to-python-object库。 这里有一个例子:

安装

pip install untangle

用法

您的xml文件(稍作更改):

<foo>
   <bar name="bar_name">
      <type foobar="1"/>
   </bar>
</foo>

使用untangle访问属性:

import untangle

obj = untangle.parse('/path_to_xml_file/file.xml')

print obj.foo.bar['name']
print obj.foo.bar.type['foobar']

输出将是:

bar_name
1

关于解缠的更多信息可以在这里找到。
另外(如果您感到好奇),您可以在此处找到用于XML和Python的工具列表(您还将看到以前的答案提到了最常见的工具)。


#12楼

我可能会建议declxml

全面披露:我之所以编写此库,是因为我正在寻找一种无需在ElementTree中编写数十行命令式解析/序列化代码的方法即可在XML和Python数据结构之间进行转换的方法。

使用declxml,您可以使用处理器以声明方式定义XML文档的结构以及如何在XML和Python数据结构之间进行映射。 处理器用于序列化和解析以及基本的验证。

解析为Python数据结构非常简单:

import declxml as xml

xml_string = """
<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>
"""

processor = xml.dictionary('foo', [
    xml.dictionary('bar', [
        xml.array(xml.integer('type', attribute='foobar'))
    ])
])

xml.parse_from_string(processor, xml_string)

产生输出:

{'bar': {'foobar': [1, 2]}}

您还可以使用同一处理器将数据序列化为XML

data = {'bar': {
    'foobar': [7, 3, 21, 16, 11]
}}

xml.serialize_to_string(processor, data, indent='    ')

产生以下输出

<?xml version="1.0" ?>
<foo>
    <bar>
        <type foobar="7"/>
        <type foobar="3"/>
        <type foobar="21"/>
        <type foobar="16"/>
        <type foobar="11"/>
    </bar>
</foo>

如果要使用对象而不是字典,则可以定义处理器以将数据与对象之间进行转换。

import declxml as xml

class Bar:

    def __init__(self):
        self.foobars = []

    def __repr__(self):
        return 'Bar(foobars={})'.format(self.foobars)


xml_string = """
<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>
"""

processor = xml.dictionary('foo', [
    xml.user_object('bar', Bar, [
        xml.array(xml.integer('type', attribute='foobar'), alias='foobars')
    ])
])

xml.parse_from_string(processor, xml_string)

产生以下输出

{'bar': Bar(foobars=[1, 2])}

#13楼

XML格式

<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>

PYTHON_CODE

import xml.etree.cElementTree as ET

tree = ET.parse("foo.xml")
root = tree.getroot() 
root_tag = root.tag
print(root_tag) 

for form in root.findall("./bar/type"):
    x=(form.attrib)
    z=list(x)
    for i in z:
        print(x[i])

输出:

foo
1
2

#14楼

xml.etree.ElementTree与lxml

这些是我在使用它们之间进行选择之前会了解的两个最常用的库的优点。

xml.etree.ElementTree:

  1. 来自标准库 :无需安装任何模块

xml文件

  1. 轻松编写XML声明 :例如,您是否需要添加standalone="no"
  2. 印刷精美:无需额外代码即可拥有漂亮的缩进 XML。
  3. 对象化功能:它使您可以像处理普通的Python对象层次结构.node一样使用XML。

#15楼

如果您使用python-benedict ,则无需使用特定于lib的api,只需从xml初始化一个新实例并轻松对其进行管理即可(因为它是dict子类)。

安装: pip install python-benedict

from benedict import benedict as bdict

# data-source can be an url, a filepath or data-string (as in this example)
data_source = """
<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>"""

data = bdict.from_xml(data_source)
t_list = data['foo.bar'] # yes, keypath supported
for t in t_list:
   print(t['@foobar'])

它支持多种格式的I / O操作并对其进行规范化: Base64CSVJSONTOMLXMLYAMLquery-string

它在GitHub上经过了良好的测试和开源: https//github.com/fabiocaccamo/python-benedict

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值