将json转换为xml格式的库。
JsonToXML的Python项目详细描述
jsontoXML
将json转换为xml格式的模块。
详细信息
模块包含将json文件/字符串转换为xml文件/字符串的函数。如果要进一步处理XML,还可以使用特定函数返回包含XML数据的指针根节点。
该模块具有以下功能:
Function | Description |
---|---|
fromFile( filename [, rootName ]) | Takes JSON data from the specified filename and returns the root pointer. |
fromText( text [, rootName ]) | Takes JSON data in form of a string and returns the root pointer. |
fromFiletoFile( inputFile, outputFile [, rootName ]) | Takes JSON data from the specified inputFile and writes the XML data to the specified outputFile. |
fromTexttoFile( text, outputFile [, rootName ]) | Takes JSON data in form of a string and writes the XML data to the specified outputFile. |
fromFiletoText( inputFile [, rootName ]) | Takes JSON data from the specified inputFile and returns the XML data in form of a string. |
fromTexttoText( text [, rootName ]) | Takes JSON data in form of a string and returns the XML data as a string. |
注意:XML数据的根节点默认为“根”,但可以通过传递参数来指定
rootName = "Custom Name"
函数。
此模块与python 3配合良好
安装
可以使用pip命令安装模块:
pip install JsonToXML
用法
模块的使用非常简单,只需导入库并直接使用函数即可。
fromfile(文件[,rootname])
import JsonToXML
import xml.etree.cElementTree as ET
import xml.dom.minidom
root = JsonToXML.fromFile("example.json", rootName="Employees") # convert the file to XML and return the root node
xmlData = ET.tostring(root, encoding='utf8',method='xml').decode() # convert the XML data to string
dom = xml.dom.minidom.parseString(xmlData)
prettyXmlData = dom.toprettyxml() # properly format the string of XML data
print(prettyXmlData) # print the formatted XML data
上面的程序从文件中读取json数据,pretty在屏幕上打印(适当缩进和换行)xml数据。
fromtext(文本[,rootname])
import JsonToXML
import xml.etree.cElementTree as ET
import xml.dom.minidom
exampleJSON = "{ 'employee': { 'name':'sonoo', 'salary':56000, 'married':true } }"
root = JsonToXML.fromText(exampleJSON) # convert the string to XML and return the root node
xmlData = ET.tostring(root, encoding='utf8',method='xml').decode() # convert the XML data to string
dom = xml.dom.minidom.parseString(xmlData)
prettyXmlData = dom.toprettyxml() # properly format the string of XML data
print(prettyXmlData) # print the formatted XML data
上面的程序从字符串中读取json并在屏幕上打印xml数据。
fromfiletofile(inputfile,outputfile[,rootname])
import JsonToXML
JsonToXML.fromFiletoFile("example1.json","example2.xml") # converts the JSON data from the file to XML and writes it on the XML file.
上面的程序只是从inputfile转换json数据,并将xml数据写入outputfile。
fromtexttofile(文本,outputfile[,rootname])
import JsonToXML
exampleJSON = "{ 'employee': { 'name':'sonoo', 'salary':56000, 'married':true } }"
JsonToXML.fromFiletoFile(exampleJSON,"example2.xml") # converts the JSON data from the string to XML and writes it on the XML file.
上面的程序只是将json数据从字符串转换并将xml数据写入outputfile。
fromfiletotext(inputfile[,rootname])
import JsonToXML
xmlData = JsonToXML.fromFiletoText("example1.json") # converts the JSON data from the file to XML and returns string of XML data.
print(xmlData)
上面的程序从文件中读取json数据并返回一个格式化的xml数据字符串。
fromtexttotext(inputfile[,rootname])
import JsonToXML
exampleJSON = "{ 'employee': { 'name':'sonoo', 'salary':56000, 'married':true } }"
xmlData = JsonToXML.fromTexttoText(exampleJSON) # converts the JSON data from the string to XML and returns string of XML data.
print(xmlData)
上面的程序从一个字符串中读取json数据并返回一个格式化的xml数据字符串。
作者
- 作者:bhavyadeep purswani
- 电子邮件:bhavyadeep.purswani98@gmail.com
- 储存库:https://github.com/bhavyadeep-purswani/JsonTOXML