<p>from lxml import etree</p>
<p>def dictlist(node):<br>res = {}<br>res[node.tag] = []<br>xmltodict(node,res[node.tag])<br>reply = {}<br>reply[node.tag] = {'value':res[node.tag],'attributes':node.attrib}<br><br>return reply</p>
<p>def xmltodict(node,res):<br>rep = {}<br><br>if len(node):<br>#n = 0<br>for n in list(node):<br>rep[node.tag] = []<br>value = xmltodict(n,rep[node.tag])<br>if len(n):<br><br>value = {'value':rep[node.tag],'attributes':n.attrib}<br>#print value<br>res.append({n.tag:value})<br>else :<br>#print rep[node.tag][0]<br>res.append(rep[node.tag][0])<br><br>else:<br>value = {}<br>value = {'value':node.text,'attributes':node.attrib}<br>#print value<br>res.append({node.tag:value})<br><br>return <br>def fromstring(strdict=None):<br>root = etree.fromstring(strdict)<br>return dictlist(root)</p>
<p>def parse(filename=None):<br>tree = etree.parse(filename)<br>return dictlist(tree.getroot())<br></p>
<p>def main():<br>tree = etree.parse('test.xml')<br>print tree<br>res = dictlist(tree.getroot())<br>print res<br><br>def dict2xml(d):<br>from xml.sax.saxutils import escape<br>def unicodify(o):<br>if o is None:<br>return u'';<br>return unicode(o)<br>lines = ["<?xml version=/"1.0/" encoding=/"utf-8/"?>"]<br>def addDict(node, offset):<br>for name, value in node.iteritems():<br>if name == "attributes":<br>strqq = lines[len(lines)-1]<br>index = strqq.find(u"<")<br>strqq = strqq[index+1:len(strqq)-1]<br>for x,y in value.iteritems():<br>strqq = strqq + u" " *4 + u"%s='%s'"%(x,y)<br>lines[len(lines)-1] = u" " * index + u"<%s>"%(strqq)<br>else:<br>if isinstance(value, dict):<br>lines.append(offset + u"<%s>" % name)<br>addDict(value, offset + u" " * 4)<br>lines.append(offset + u"</%s>" % name)<br>elif isinstance(value, list):<br>for item in value:<br>if isinstance(item, dict):<br>addDict(item, offset + u" " * 4)<br>else:<br>lines.append(offset + u"<%s>%s</%s>" % (name, escape(unicodify(item)), name))<br>else:<br>if value != "":<br>pass<br>addDict(d, u"")<br>lines.append(u"")<br>return u"/n".join(lines)<br><br>#if __name__ == '__main__' :<br>#main()</p>
<p>test 就不帮大家写了</p>
<p>def dictlist(node):<br>res = {}<br>res[node.tag] = []<br>xmltodict(node,res[node.tag])<br>reply = {}<br>reply[node.tag] = {'value':res[node.tag],'attributes':node.attrib}<br><br>return reply</p>
<p>def xmltodict(node,res):<br>rep = {}<br><br>if len(node):<br>#n = 0<br>for n in list(node):<br>rep[node.tag] = []<br>value = xmltodict(n,rep[node.tag])<br>if len(n):<br><br>value = {'value':rep[node.tag],'attributes':n.attrib}<br>#print value<br>res.append({n.tag:value})<br>else :<br>#print rep[node.tag][0]<br>res.append(rep[node.tag][0])<br><br>else:<br>value = {}<br>value = {'value':node.text,'attributes':node.attrib}<br>#print value<br>res.append({node.tag:value})<br><br>return <br>def fromstring(strdict=None):<br>root = etree.fromstring(strdict)<br>return dictlist(root)</p>
<p>def parse(filename=None):<br>tree = etree.parse(filename)<br>return dictlist(tree.getroot())<br></p>
<p>def main():<br>tree = etree.parse('test.xml')<br>print tree<br>res = dictlist(tree.getroot())<br>print res<br><br>def dict2xml(d):<br>from xml.sax.saxutils import escape<br>def unicodify(o):<br>if o is None:<br>return u'';<br>return unicode(o)<br>lines = ["<?xml version=/"1.0/" encoding=/"utf-8/"?>"]<br>def addDict(node, offset):<br>for name, value in node.iteritems():<br>if name == "attributes":<br>strqq = lines[len(lines)-1]<br>index = strqq.find(u"<")<br>strqq = strqq[index+1:len(strqq)-1]<br>for x,y in value.iteritems():<br>strqq = strqq + u" " *4 + u"%s='%s'"%(x,y)<br>lines[len(lines)-1] = u" " * index + u"<%s>"%(strqq)<br>else:<br>if isinstance(value, dict):<br>lines.append(offset + u"<%s>" % name)<br>addDict(value, offset + u" " * 4)<br>lines.append(offset + u"</%s>" % name)<br>elif isinstance(value, list):<br>for item in value:<br>if isinstance(item, dict):<br>addDict(item, offset + u" " * 4)<br>else:<br>lines.append(offset + u"<%s>%s</%s>" % (name, escape(unicodify(item)), name))<br>else:<br>if value != "":<br>pass<br>addDict(d, u"")<br>lines.append(u"")<br>return u"/n".join(lines)<br><br>#if __name__ == '__main__' :<br>#main()</p>
<p>test 就不帮大家写了</p>