将json转换为xml格式的库。

JsonToXML的Python项目详细描述


jsontoXML

将json转换为xml格式的模块。

详细信息

模块包含将json文件/字符串转换为xml文件/字符串的函数。如果要进一步处理XML,还可以使用特定函数返回包含XML数据的指针根节点。

该模块具有以下功能:

FunctionDescription
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数据字符串。

作者

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java什么数据库最类似于Map,每个用户/id存储无限多个“键”和“值”?   java仅使用super pom进行测试   内存不足如何解析java。OutOfMemoryError:Java堆空间在增加堆大小的情况下将意味着延迟OutOfMemoryError   来自另一个类的mysql和java jdbc调用[运行时应用程序]   java通过下拉菜单更改搜索框搜索的内容   JAVAlang.ClassNotFoundException:sun。jdbc。odbc。JdbcOdbcDriver   java Selenium点击链接   JavaSpringHibernate:从唯一值列表中获取对象列表   java Bing广告与桌面身份验证问题   java如何在没有任何外部SDK的情况下从安卓打印到收据打印机?   未调用java菜单片段类   java在IDEA和PyCharm中同时为同一个项目工作   java我们如何为同一个异常提供不同的海关信息   jakarta ee中是否预定义了“请求”和“响应”变量或值?   java更好地解决“之前和之后”难题?   尝试将数据从Excel添加到Java   发送电子邮件的Java代码只适用于一个电子邮件id?   java如何从资产解析XML?