def xmlfile2dict(xmlfile, enc='utf-8'): """convert xml to dict with encoding >>> xmlfile = './testxml.xml' >>> xml_dict = myxml2dict(xmlfile, enc='utf-8') """ tree = etree.parse(xmlfile) xml_str = etree.tostring(tree, encoding=enc) dxml = xml2dict.XML2Dict() return dxml.fromstring(xml_str)
def Info(self): cmd = 'svn info --xml %s' % self.local_path rs = self._DoCmd(cmd) info_xml = ''.join(rs) info_xml = info_xml.replace('>\n', '>') x2d = xml2dict.XML2Dict() info_data = x2d.fromstring(info_xml) infos = self.__convert_data(info_data['info']['entry']) return infos
def URL2XML2Dict(url): xmldict = xml2dict.XML2Dict() f = URL2File(url) tree = dom.parse(f) f.close() header = f.info().gettype() assert 'xml' in header for node in tree.childNodes: if node.nodeType == dom.Node.ELEMENT_NODE: body = node.toxml('utf-8') xmldict = xmldict.fromstring(body) return xmldict
def Log(self, start_rev, end_rev='HEAD'): cmd = 'svn log --xml -r %s:%s %s' % (start_rev, end_rev, self.local_path) rs = self._DoCmd(cmd) xml = ''.join(rs) xml = xml.replace('>\n', '>') x2d = xml2dict.XML2Dict() log_data = x2d.fromstring(xml) logs = [] raw_logs = log_data['log']['logentry'] if not isinstance(raw_logs, list): raw_logs = [raw_logs] for log in raw_logs: logs.append(self.__convert_data(log)) return logs
def GetDictsByXStr(self, xmlstr): xml = xml2dict.XML2Dict() r = xml.fromstring(xmlstr) #pprint(r) return r
return nodelist, attrs def process_simple(doc, tag, v): """For int, str Return node """ node = doc.createElement(tag) #node.appendChild(doc.createTextNode(str(v))) node.appendChild(doc.createTextNode(unicode(v))) return node def dict2xml(d): """Generate a xml string from dict""" doc = impl.createDocument(None, None, None) if len(d) > 1: print 'Only one root node allowed' return None root, _ = process_complex(doc, d.items()) doc.appendChild(root[0]) return doc.toprettyxml(encoding='utf-8', indent=' ') if __name__ == '__main__': import xml2dict x = xml2dict.XML2Dict() d = x.parse('t5') print d print dict2xml(d)
def xmlstr2dict(xml_str): dxml = xml2dict.XML2Dict() return dxml.fromstring(xml_str)