主要对ElementTree和minidom写xml文件做了一下测试。
ElementTree比较简单,使用起来也很方便,但是在生成xml文件时候有一个格式问题,生成的文件整个文本在一行中显示,阅读很不方便,我这里通过一些计算改造 text 和 tail 节点达到了格式化的目的。
minidom 看起来比较重量级,在格式化生成xml 文件时表现较好,传入合适的参数就能直接生成想要的格式的数据。
XmlETreeTest.py
#!python2
#!coding:utf-8
import xml.etree.ElementTree as ET
def PrintXml(root):
print root.tag, root.attrib, root.text
for elem in root:
PrintXml(elem)
def ReadAndIterXml(root):
print root.tag, root.attrib
for child in root:
print child.tag, child.attrib
for neighbor in root.iter('neighbor'):
print neighbor.attrib
for country in root.findall('country'):
rank = country.find('rank').text
name = country.get('name')
print name, rank
def ModifyXml(tree, root, filename):
for rank in root.iter('rank'):
new_rank = int(rank.text) + 1
rank.text = str(new_rank)
rank.set('updated', 'yes')
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
tree.write(filename)
def CreateXml(filename):
root = ET.Element('data')
tree = ET.ElementTree(root)
country = ET.Element('country')
country.set("name", "Liechtenstein")
rank = ET.Element('rank')
rank.text = "1"
year = ET.Element('year')
year.text = "2008"
gdppc = ET.Element('gdppc')
gdppc.text = "141100"
neighbor1 = ET.Element('neighbor')
neighbor1.set("name", "Austria")
neighbor1.set("direction", "E")
neighbor2 = ET.Element('neighbor')
neighbor2.set("name", "Switzerland")
neighbor2.set("direction", "W")
country.append(rank)
country.append(year)
country.append(gdppc)
country.append(neighbor1)
country.append(neighbor2)
country2 = ET.Element('country')
country2.set("name", "Singapore")
rank = ET.Element('rank')
rank.text = "4"
year = ET.Element('year')
year.text = "2011"
gdppc = ET.Element('gdppc')
gdppc.text = "59900"
neighbor1 = ET.Element('neighbor')
neighbor1.set("name", "Malaysia")
neighbor1.set("direction", "N")
country2.append(rank)
country2.append(year)
country2.append(gdppc)
country2.append(neighbor1)
country3 = ET.Element('country')
country3.set("name", "Singapore")
rank = ET.Element('rank')
rank.text = "68"
year = ET.Element('year')
year.text = "2011"
gdppc = ET.Element('gdppc')
gdppc.text = "13600"
neighbor1 = ET.Element('neighbor')
neighbor1.set("name", "Costa Rica")
neighbor1.set("direction", "W")
neighbor2 = ET.Element('neighbor')
neighbor2.set("name", "Colombia")
neighbor2.set("direction", "E")
country3.append(rank)
country3.append(year)
country3.append(gdppc)
country3.append(neighbor1)
country3.append(neighbor2)
root.append(country)
root.append(country2)
root.append(country3)
Indent(root)
#PrintXml(root)
tree.write(filename, encoding='utf-8', xml_declaration=True)
def Indent(elem, level=0, isLast=True):
textIndent = "\n" + (level + 1)*"\t"
tailIndent = "\n" + level*"\t"
if isLast:
tailIndent = "\n" + (level - 1)*"\t"
if len(elem) > 0 and elem.text is None:
elem.text = textIndent
elem.tail = tailIndent
i = 0
length = len(elem)
for child in elem:
i += 1
Indent(child, level + 1, (i == length))
def main():
tree = ET.parse('ETreeRead.xml')
root = tree.getroot()
ReadAndIterXml(root)
ModifyXml(tree, root, "ETreeModify.xml")
CreateXml("ETreeCreate.xml")
if __name__ == '__main__':
main()
生成的xml文件:
1
2008
141100
4
2011
59900
68
2011
13600
XmlDomTest.py
#!python2
#!coding:utf-8
import xml.dom.minidom
def CreateXml(filename):
impl = xml.dom.minidom.getDOMImplementation()
dom = impl.createDocument(None, 'root', None)
root = dom.documentElement
country = dom.createElement('country')
country.setAttribute("name", "Liechtenstein")
rank = dom.createElement('rank')
rankText = dom.createTextNode('1')
rank.appendChild(rankText)
year = dom.createElement('year')
yearText = dom.createTextNode('2008')
year.appendChild(yearText)
gdppc = dom.createElement('gdppc')
gdppcText = dom.createTextNode('141100')
gdppc.appendChild(gdppcText)
neighbor1 = dom.createElement('neighbor')
neighbor1.setAttribute("name", "Austria")
neighbor1.setAttribute("direction", "E")
neighbor2 = dom.createElement('neighbor')
neighbor2.setAttribute("name", "Switzerland")
neighbor2.setAttribute("direction", "W")
country.appendChild(rank)
country.appendChild(year)
country.appendChild(gdppc)
country.appendChild(neighbor1)
country.appendChild(neighbor2)
country2 = dom.createElement('country')
country2.setAttribute("name", "Singapore")
rank = dom.createElement('rank')
rankText = dom.createTextNode('4')
rank.appendChild(rankText)
year = dom.createElement('year')
yearText = dom.createTextNode('2011')
year.appendChild(yearText)
gdppc = dom.createElement('gdppc')
gdppcText = dom.createTextNode('59900')
gdppc.appendChild(gdppcText)
neighbor1 = dom.createElement('neighbor')
neighbor1.setAttribute("name", "Malaysia")
neighbor1.setAttribute("direction", "N")
country2.appendChild(rank)
country2.appendChild(year)
country2.appendChild(gdppc)
country2.appendChild(neighbor1)
country3 = dom.createElement('country')
country3.setAttribute("name", "Panama")
rank = dom.createElement('rank')
rankText = dom.createTextNode('68')
rank.appendChild(rankText)
year = dom.createElement('year')
yearText = dom.createTextNode('2008')
year.appendChild(yearText)
gdppc = dom.createElement('gdppc')
gdppcText = dom.createTextNode('13600')
gdppc.appendChild(gdppcText)
neighbor1 = dom.createElement('neighbor')
neighbor1.setAttribute("name", "Costa Rica")
neighbor1.setAttribute("direction", "W")
neighbor2 = dom.createElement('neighbor')
neighbor2.setAttribute("name", "Colombia")
neighbor2.setAttribute("direction", "E")
country3.appendChild(rank)
country3.appendChild(year)
country3.appendChild(gdppc)
country3.appendChild(neighbor1)
country3.appendChild(neighbor2)
root.appendChild(country)
root.appendChild(country2)
root.appendChild(country3)
f= open(filename, 'w')
dom.writexml(f, indent="", addindent='\t', newl='\n', encoding="utf8")
f.close()
def main():
CreateXml("DomCreate.xml")
if __name__ == "__main__":
main()
生成的xml文件:
1
2008
141100
4
2011
59900
68
2008
13600