python xml2json

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python xml2json相关的知识,希望对你有一定的参考价值。

import json
import io
from lxml import etree 

def read_xml(file_name):
    in_file = open(file_name,"rb")
    xml_file = etree.parse(in_file)
    return xml_file

def generate_rterms_dic(xml_element):
    ##Something something darkside
    entry = []
    for rterm in xml_element:
        dict_term = dict()
        for rentry in rterm.getchildren():
            dict_term[rentry.tag] = rentry.text
        entry.append(dict_term)
    return entry

def generate_dic_entry(xml_element):
    terms = dict()
    for rtags in xml_element.getchildren():
        if rtags.tag == "RelatedTerms":
            terms["RelatedTerms"] = generate_rterms_dic(rtags)
        else:
            terms[rtags.tag] = rtags.text
    return terms

def convertXml2Json(file_name,json_filename):
   tree =  read_xml(file_name)
   tree_root = tree.getroot()
   json_out = dict()
   ths_array = []
   json_out["thesaurus"] = ths_array
   for term in tree_root:
       ths_array.append(generate_dic_entry(term))
   with open(json_filename,"w") as write_file:
       generated_json = json.dumps(json_out, indent=2,sort_keys=False)
       write_file.write(generated_json)
       write_file.close()

if __name__ == "__main__":
    convertXml2Json('/Users/myrddian/Projects/MonashGDDS/datawrangling/xml_parsing/xml.xml', "json.dat")

以上是关于python xml2json的主要内容,如果未能解决你的问题,请参考以下文章

使用 $.xml2Json 返回一个复杂的对象

为 API 调用 Xml2Json 制作 Json 模型

C# xml2json

python解析xml文件

vue中引入x2js(针对xml、json数据进行转换处理)

Jackson 学习笔记 XML转JSON