I'm looking for an XML to dictionary parser using ElementTree, I already found some but they are excluding the attributes, and in my case I have a lot of attributes.
我正在寻找使用ElementTree的XML到字典解析器,我已经找到了一些,但是它们排除了属性,在我的例子中,我有很多属性。
8 个解决方案
#1
22
def etree_to_dict(t):
d = {t.tag : map(etree_to_dict, t.iterchildren())}
d.update(('@' + k, v) for k, v in t.attrib.iteritems())
d['text'] = t.text
return d
Call as
电话是
tree = etree.parse("some_file.xml")
etree_to_dict(tree.getroot())
This works as long as you don't actually have an attribute text
; if you do, then change the third line in the function body to use a different key. Also, you can't handle mixed content with this.
这是可行的,只要你没有一个属性文本;如果您这样做,则在函数体中更改第三行,以使用不同的键。此外,您不能处理与此混合的内容。
(Tested on LXML.)
(LXML测试)。
#2
27
The following XML-to-Python-dict snippet parses entities as well as attributes following this XML-to-JSON "specification":
下面的xml -to- pythondict代码片段解析了实体以及遵循这个XML-to-JSON“规范”的属性:
from collections import defaultdict
def etree_to_dict(t):
d = {t.tag: {} if t.attrib else None}
children = list(t)
if children:
dd = defaultdict(list)
for dc in map(etree_to_dict, children):
for k, v in dc.items():
dd[k].append(v)
d = {t.tag: {k: v[0] if len(v) == 1 else v
for k, v in dd.items()}}
if t.attrib:
d[t.tag].update(('@' + k, v)
for k, v in t.attrib.items())
if t.text:
text = t.text.strip()
if children or t.attrib:
if text:
d[t.tag]['#text'] = text
else:
d[t.tag] = text
return d
It is used:
使用它:
from xml.etree import cElementTree as ET
e = ET.XML('''
<root>
<e />
<e>text</e>
<e name="value" />
<e name="value">text</e>
<e> <a>text</a> <b>text</b> </e>
<e> <a>text</a> <a>text</a> </e>
<e> text <a>text</a> </e>
</root>
''')
from pprint import pprint
d = etree_to_dict(e)
pprint(d)
The output of this example (as per above-linked "specification") should be:
此示例的输出(按照上述链接的“规范”)应该是:
{'root': {'e': [None,
'text',
{'@name': 'value'},
{'#text': 'text', '@name': 'value'},
{'a': 'text', 'b': 'text'},
{'a': ['text', 'text']},
{'#text': 'text', 'a': 'text'}]}}
Not necessarily pretty, but it is unambiguous, and simpler XML inputs result in simpler JSON. :)
不一定很好,但是它是明确的,更简单的XML输入导致更简单的JSON。:)
Update
If you want to do the reverse, emit an XML string from a JSON/dict, you can use:
如果您想做相反的事情,从JSON/dict类型中发出XML字符串,您可以使用:
try:
basestring
except NameError: # python3
basestring = str
def dict_to_etree(d):
def _to_etree(d, root):
if not d:
pass
elif isinstance(d, str):
root.text = d
elif isinstance(d, dict):
for k,v in d.items():
assert isinstance(k, str)
if k.startswith('#'):
assert k == '#text' and isinstance(v, str)
root.text = v
elif k.startswith('@'):
assert isinstance(v, str)
root.set(k[1:], v)
elif isinstance(v, list):
for e in v:
_to_etree(e, ET.SubElement(root, k))
else:
_to_etree(v, ET.SubElement(root, k))
else:
assert d == 'invalid type', (type(d), d)
assert isinstance(d, dict) and len(d) == 1
tag, body = next(iter(d.items()))
node = ET.Element(tag)
_to_etree(body, node)
return node
print(ET.tostring(dict_to_etree(d)))
#3
2
Based on @larsmans, if you don't need attributes, this will give you a tighter dictionary --
基于@larsmans,如果你不需要属性,这将给你一个更紧凑的字典—
def etree_to_dict(t):
return {t.tag : map(etree_to_dict, t.iterchildren()) or t.text}
#4
1
Here is a simple data structure in xml (save as file.xml):
下面是xml中的一个简单数据结构(save as file.xml):
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Person>
<First>John</First>
<Last>Smith</Last>
</Person>
<Person>
<First>Jane</First>
<Last>Doe</Last>
</Person>
</Data>
Here is the code to create a list of dictionary objects from it.
下面是创建字典对象列表的代码。
from lxml import etree
tree = etree.parse('file.xml')
root = tree.getroot()
datadict = []
for item in root:
d = {}
for elem in item:
d[elem.tag]=elem.text
datadict.append(d)
datadict now contains:
datadict现在包含:
[{'First': 'John', 'Last': 'Smith'},{'First': 'Jane', 'Last': 'Doe'}]
and can be accessed like so:
可以这样访问:
datadict[0]['First']
'John'
datadict[1]['Last']
'Doe'
#5
1
For transforming XML from/to python dictionaries, xmltodict has worked great for me:
对于将XML从/转换到python字典,xmltodict对于我来说非常有用:
import xmltodict
xml = '''
<root>
<e />
<e>text</e>
<e name="value" />
<e name="value">text</e>
<e> <a>text</a> <b>text</b> </e>
<e> <a>text</a> <a>text</a> </e>
<e> text <a>text</a> </e>
</root>
'''
xdict = xmltodict.parse(xml)
xdict will now look like
xdict现在看起来像
OrderedDict([('root',
OrderedDict([('e',
[None,
'text',
OrderedDict([('@name', 'value')]),
OrderedDict([('@name', 'value'),
('#text', 'text')]),
OrderedDict([('a', 'text'), ('b', 'text')]),
OrderedDict([('a', ['text', 'text'])]),
OrderedDict([('a', 'text'),
('#text', 'text')])])]))])
If your XML data is not in raw string/bytes form but in some ElementTree object, you just need to print it out as a string and use xmldict.parse again. For instance, if you are using lxml to process the XML documents, then
如果您的XML数据不是以原始字符串/字节的形式,而是以某种ElementTree对象的形式,那么只需将其作为字符串打印出来,并使用xmldict来。解析了。例如,如果您正在使用lxml处理XML文档,那么
from lxml import etree
e = etree.XML(xml)
xmltodict.parse(etree.tostring(e))
will produce the same dictionary as above.
将产生与上面相同的字典。
#6
0
from lxml import etree, objectify
def formatXML(parent):
"""
Recursive operation which returns a tree formated
as dicts and lists.
Decision to add a list is to find the 'List' word
in the actual parent tag.
"""
ret = {}
if parent.items(): ret.update(dict(parent.items()))
if parent.text: ret['__content__'] = parent.text
if ('List' in parent.tag):
ret['__list__'] = []
for element in parent:
ret['__list__'].append(formatXML(element))
else:
for element in parent:
ret[element.tag] = formatXML(element)
return ret
#7
0
Building on @larsmans, if the resulting keys contain xml namespace info, you can remove that before writing to the dict. Set a variable xmlns
equal to the namespace and strip its value out.
在@larsmans的基础上,如果产生的键包含xml名称空间信息,可以在写入到dict词前删除它。
xmlns = '{http://foo.namespaceinfo.com}'
def etree_to_dict(t):
if xmlns in t.tag:
t.tag = t.tag.lstrip(xmlns)
if d = {t.tag : map(etree_to_dict, t.iterchildren())}
d.update(('@' + k, v) for k, v in t.attrib.iteritems())
d['text'] = t.text
return d
#8
0
You can use this snippet that directly converts it from xml to dictionary
您可以使用这个片段直接将它从xml转换为dictionary
import xml.etree.ElementTree as ET
xml = ('<xml>' +
'<first_name>Dean Christian</first_name>' +
'<middle_name>Christian</middle_name>' +
'<last_name>Armada</last_name>' +
'</xml>')
root = ET.fromstring(xml)
x = {x.tag: root.find(x.tag).text for x in root._children}
# returns {'first_name': 'Dean Christian', 'last_name': 'Armada', 'middle_name': 'Christian'}
#1
22
def etree_to_dict(t):
d = {t.tag : map(etree_to_dict, t.iterchildren())}
d.update(('@' + k, v) for k, v in t.attrib.iteritems())
d['text'] = t.text
return d
Call as
电话是
tree = etree.parse("some_file.xml")
etree_to_dict(tree.getroot())
This works as long as you don't actually have an attribute text
; if you do, then change the third line in the function body to use a different key. Also, you can't handle mixed content with this.
这是可行的,只要你没有一个属性文本;如果您这样做,则在函数体中更改第三行,以使用不同的键。此外,您不能处理与此混合的内容。
(Tested on LXML.)
(LXML测试)。
#2
27
The following XML-to-Python-dict snippet parses entities as well as attributes following this XML-to-JSON "specification":
下面的xml -to- pythondict代码片段解析了实体以及遵循这个XML-to-JSON“规范”的属性:
from collections import defaultdict
def etree_to_dict(t):
d = {t.tag: {} if t.attrib else None}
children = list(t)
if children:
dd = defaultdict(list)
for dc in map(etree_to_dict, children):
for k, v in dc.items():
dd[k].append(v)
d = {t.tag: {k: v[0] if len(v) == 1 else v
for k, v in dd.items()}}
if t.attrib:
d[t.tag].update(('@' + k, v)
for k, v in t.attrib.items())
if t.text:
text = t.text.strip()
if children or t.attrib:
if text:
d[t.tag]['#text'] = text
else:
d[t.tag] = text
return d
It is used:
使用它:
from xml.etree import cElementTree as ET
e = ET.XML('''
<root>
<e />
<e>text</e>
<e name="value" />
<e name="value">text</e>
<e> <a>text</a> <b>text</b> </e>
<e> <a>text</a> <a>text</a> </e>
<e> text <a>text</a> </e>
</root>
''')
from pprint import pprint
d = etree_to_dict(e)
pprint(d)
The output of this example (as per above-linked "specification") should be:
此示例的输出(按照上述链接的“规范”)应该是:
{'root': {'e': [None,
'text',
{'@name': 'value'},
{'#text': 'text', '@name': 'value'},
{'a': 'text', 'b': 'text'},
{'a': ['text', 'text']},
{'#text': 'text', 'a': 'text'}]}}
Not necessarily pretty, but it is unambiguous, and simpler XML inputs result in simpler JSON. :)
不一定很好,但是它是明确的,更简单的XML输入导致更简单的JSON。:)
Update
If you want to do the reverse, emit an XML string from a JSON/dict, you can use:
如果您想做相反的事情,从JSON/dict类型中发出XML字符串,您可以使用:
try:
basestring
except NameError: # python3
basestring = str
def dict_to_etree(d):
def _to_etree(d, root):
if not d:
pass
elif isinstance(d, str):
root.text = d
elif isinstance(d, dict):
for k,v in d.items():
assert isinstance(k, str)
if k.startswith('#'):
assert k == '#text' and isinstance(v, str)
root.text = v
elif k.startswith('@'):
assert isinstance(v, str)
root.set(k[1:], v)
elif isinstance(v, list):
for e in v:
_to_etree(e, ET.SubElement(root, k))
else:
_to_etree(v, ET.SubElement(root, k))
else:
assert d == 'invalid type', (type(d), d)
assert isinstance(d, dict) and len(d) == 1
tag, body = next(iter(d.items()))
node = ET.Element(tag)
_to_etree(body, node)
return node
print(ET.tostring(dict_to_etree(d)))
#3
2
Based on @larsmans, if you don't need attributes, this will give you a tighter dictionary --
基于@larsmans,如果你不需要属性,这将给你一个更紧凑的字典—
def etree_to_dict(t):
return {t.tag : map(etree_to_dict, t.iterchildren()) or t.text}
#4
1
Here is a simple data structure in xml (save as file.xml):
下面是xml中的一个简单数据结构(save as file.xml):
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Person>
<First>John</First>
<Last>Smith</Last>
</Person>
<Person>
<First>Jane</First>
<Last>Doe</Last>
</Person>
</Data>
Here is the code to create a list of dictionary objects from it.
下面是创建字典对象列表的代码。
from lxml import etree
tree = etree.parse('file.xml')
root = tree.getroot()
datadict = []
for item in root:
d = {}
for elem in item:
d[elem.tag]=elem.text
datadict.append(d)
datadict now contains:
datadict现在包含:
[{'First': 'John', 'Last': 'Smith'},{'First': 'Jane', 'Last': 'Doe'}]
and can be accessed like so:
可以这样访问:
datadict[0]['First']
'John'
datadict[1]['Last']
'Doe'
#5
1
For transforming XML from/to python dictionaries, xmltodict has worked great for me:
对于将XML从/转换到python字典,xmltodict对于我来说非常有用:
import xmltodict
xml = '''
<root>
<e />
<e>text</e>
<e name="value" />
<e name="value">text</e>
<e> <a>text</a> <b>text</b> </e>
<e> <a>text</a> <a>text</a> </e>
<e> text <a>text</a> </e>
</root>
'''
xdict = xmltodict.parse(xml)
xdict will now look like
xdict现在看起来像
OrderedDict([('root',
OrderedDict([('e',
[None,
'text',
OrderedDict([('@name', 'value')]),
OrderedDict([('@name', 'value'),
('#text', 'text')]),
OrderedDict([('a', 'text'), ('b', 'text')]),
OrderedDict([('a', ['text', 'text'])]),
OrderedDict([('a', 'text'),
('#text', 'text')])])]))])
If your XML data is not in raw string/bytes form but in some ElementTree object, you just need to print it out as a string and use xmldict.parse again. For instance, if you are using lxml to process the XML documents, then
如果您的XML数据不是以原始字符串/字节的形式,而是以某种ElementTree对象的形式,那么只需将其作为字符串打印出来,并使用xmldict来。解析了。例如,如果您正在使用lxml处理XML文档,那么
from lxml import etree
e = etree.XML(xml)
xmltodict.parse(etree.tostring(e))
will produce the same dictionary as above.
将产生与上面相同的字典。
#6
0
from lxml import etree, objectify
def formatXML(parent):
"""
Recursive operation which returns a tree formated
as dicts and lists.
Decision to add a list is to find the 'List' word
in the actual parent tag.
"""
ret = {}
if parent.items(): ret.update(dict(parent.items()))
if parent.text: ret['__content__'] = parent.text
if ('List' in parent.tag):
ret['__list__'] = []
for element in parent:
ret['__list__'].append(formatXML(element))
else:
for element in parent:
ret[element.tag] = formatXML(element)
return ret
#7
0
Building on @larsmans, if the resulting keys contain xml namespace info, you can remove that before writing to the dict. Set a variable xmlns
equal to the namespace and strip its value out.
在@larsmans的基础上,如果产生的键包含xml名称空间信息,可以在写入到dict词前删除它。
xmlns = '{http://foo.namespaceinfo.com}'
def etree_to_dict(t):
if xmlns in t.tag:
t.tag = t.tag.lstrip(xmlns)
if d = {t.tag : map(etree_to_dict, t.iterchildren())}
d.update(('@' + k, v) for k, v in t.attrib.iteritems())
d['text'] = t.text
return d
#8
0
You can use this snippet that directly converts it from xml to dictionary
您可以使用这个片段直接将它从xml转换为dictionary
import xml.etree.ElementTree as ET
xml = ('<xml>' +
'<first_name>Dean Christian</first_name>' +
'<middle_name>Christian</middle_name>' +
'<last_name>Armada</last_name>' +
'</xml>')
root = ET.fromstring(xml)
x = {x.tag: root.find(x.tag).text for x in root._children}
# returns {'first_name': 'Dean Christian', 'last_name': 'Armada', 'middle_name': 'Christian'}