如何将xml文件转换为json文件

前端之家收集整理的这篇文章主要介绍了如何将xml文件转换为json文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道如何将我的 Linux目录中的提取数据保存到json.我尝试将其保存到XML文件中,如果我运行脚本,它将生成一个XML(data.xml).现在,我不知道如何在json文件中.

这是我的bash脚本中的XML模板:

file=~/data.xml
  template=" <Data>\n\
  <date>%d</date>\n\
  <time>%d</time>\n\
  <age>%d</age>\n\
  <place>%s</place>\n\
  <name>%d<name>\n\</Data>\n"

  echo '<?xml version="1.0"?>' >> $file
  echo '<List2>' >> $file

模板

printf "$template" "$date"\
                    "$time"\
                    "$age"\
                    "$place"\
                    "$name"\$file   
#closing tag
echo '</List>' >> $file

解决方法

我建议使用 xmljson lib和代码……像这样:

from collections import OrderedDict
from xmljson import BadgerFish
from json import dumps

def xml_to_json(xml_string):
    bf = BadgerFish(dict_type=OrderedDict)
    return dumps(bf.data(fromstring(xml_string)))

猜你在找的XML相关文章