使用 python 从 XSD 文件创建特定的 XML 文件

ser*_*ajo 7 xml xsd python-3.x

我有一个现有的 xsd 架构,并且需要创建(希望使用 Python)带有一些特定输入的 XML 文件。最好的方法是什么?我尝试了 Element Tree 和 xmlschema,但我无法判断它们是否允许从已知的 XSD 架构开始生成 XML 文件。谢谢

ser*_*ajo 2

最后我做了以下工作并且似乎完成了这项工作:

import xmlschema
from xml.etree.ElementTree import ElementTree
# create XML from json, starting from known schema file 
sch      = 'schema_file.xsd'
schema   = xmlschema.XMLSchema(sch) 
jsondata = json of the data to be converted to XML
xml      = xmlschema.from_json(jsondata,schema=schema)
# write to XML
ElementTree(xml).write('myxml.xml')
Run Code Online (Sandbox Code Playgroud)

  • 您如何知道 JSON 数据是什么样子?这就是我目前绊倒的地方。 (3认同)
  • [有关将模式解码为 JSON 的 xmlschema 文档](https://xmlschema.readthedocs.io/en/latest/usage.html#decoding-to-json) 应该可以让您了解 JSON 的外观。 (2认同)