python - 如何将完整的 JSON 表单转换为 XML
问题描述
如果我有以下代码并且想要转换为 XML: 注意:我尝试使用 json2xml,但它不会转换完整的集合,而只是转换它的一部分。
{
"odoo": {
"data": {
"record": [
{
"model": "ir.ui.view",
"id": "lab_tree_view",
"field": [
{
"name": "name",
"#text": "human.name.tree"
},
{
"name": "model",
"#text": "human.name"
},
{
"name": "priority",
"eval": "16"
},
{
"name": "arch",
"type": "xml",
"tree": {
"string": "Human Name",
"field": [
{"name": "name"},
{"name": "family"},
{"name": "given"},
{"name": "prefix"}
]
}
}
]
},
{
"model": "ir.ui.view",
"id": "human_name_form_view",
"field": [
{
"name": "name",
"#text": "human.name.form"
},
{
"name": "model",
"#text": "human.name"
},
{
"name": "arch",
"type": "xml",
"form": {
"string": "Human Name Form",
"sheet": {
"group": {
"field": [
{"name": "name"},
{"name": "family"},
{"name": "given"},
{"name": "prefix"}
]
}
}
}
}
]
}
],
"#text": "\n\n\n #ACTION_WINDOW_FOR_PATIENT\n ",
"record#1": {
"model": "ir.actions.act_window",
"id": "action_human_name",
"field": [
{
"name": "name",
"#text": "Human Name"
},
{
"name": "res_model",
"#text": "human.name"
},
{
"name": "view_mode",
"#text": "tree,form"
},
{
"name": "help",
"type": "html",
"p": {
"class": "o_view_nocontent_smiling_face",
"#text": "Create the Human Name\n "
}
}
]
},
"menuitem": [
{
"id": "FHIR_root",
"name": "FHIR"
},
{
"id": "FHIR_human_name",
"name": "Human Name",
"parent": "FHIR_root",
"action": "action_human_name"
}
]
}
}
}
是否有任何 Python 库或专用代码来执行此操作?
我尝试构建自定义函数来解决这个问题并将它们全部转换,但是,我陷入了这个问题。这里的用例是输入上面的代码,输出应该是任何在线转换器生成的代码
编辑:
from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjson
data = readfromstring(string)
print(json2xml.Json2xml(data).to_xml()
上面的代码只将 json 的一部分像下面的代码转换为 xml:
{
"record": {
"model": "ir.ui.view",
"id": "address_tree_view",
"field": [
{
"name": "name",
"#text": "address.tree.view"
},
{
"name": "model",
"#text": "address"
},
{
"name": "priority",
"eval": "16"
},
{
"name": "arch",
"type": "xml",
"tree": {
"string": "Address",
"field": [
{
"name": "text_address"
},
{
"name": "address_line1"
},
{
"name": "country_id"
},
{
"name": "state_id"
},
{
"name": "address_district"
},
{
"name": "address_city"
},
{
"name": "address_postal_code"
}
]
}
}
]
}
}
PS:我已经使用了在线转换器,但是我不想在这里这样做。
解决方案
使用 dicttoxml 将 JSON 直接转换为 XML
安装 pip install dicttoxml 或 easy_install dicttoxml
In [2]: from json import loads
In [3]: from dicttoxml import dicttoxml
In [4]: json_obj = '{"main" : {"aaa" : "10", "bbb" : [1,2,3]}}'
In [5]: xml = dicttoxml(loads(json_obj))
In [6]: print(xml)
<?xml version="1.0" encoding="UTF-8" ?><root><main type="dict"><aaa type="str">10</aaa><bbb type="list"><item type="int">1</item><item type="int">2</item><item type="int">3</item></bbb></main></root>
In [7]: xml = dicttoxml(loads(json_obj), attr_type=False)
In [8]: print(xml)
<?xml version="1.0" encoding="UTF-8" ?><root><main><aaa>10</aaa><bbb><item>1</item><item>2</item><item>3</item></bbb></main></root>
有关更多信息,请查看此处
推荐阅读
- bert-language-model - 是否可以使用带有 BERT-Large 而不是 BERT-base 的 AllenNLP Semantic Role Labeler?
- curl - 根据 Twitch 用户名和密码获取用户的流密钥?
- javascript - 在 VSCode 中无法解决 ESLint 的问题
- maven - 如何在根项目目录文件夹中获取多模块项目的最终结果?
- c# - 从应用程序到网络打印机的延迟打印
- android - 有人可以帮助我更好地理解 Room 持久性库吗?
- python - 如何附加到列表但跳过一行?
- r - 绘制 y 和多个 x 之间的关系?
- svg - 带有暂停的 SVG 渐变动画循环
- owl - 如何通过 SHACL 断言逆三元组(假设它们还不存在?)