使用 python 从 XSD 文件创建特定的 XML 文件 [英] create specific XML file from XSD file with python

查看:62
本文介绍了使用 python 从 XSD 文件创建特定的 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an existing xsd schema, and need to create (hopefully with Python) an XML file with some specific inputs. What is the best way to do it? I tried Element Tree and xmlschema, but I cannot tell if these allow to generate XML files starting from a known XSD schema. Thanks

推荐答案

这里是一个使用 json 数据的最小工作示例

here is a minimal working example with json data

import xmlschema
import json
from xml.etree.ElementTree import ElementTree

my_xsd = '<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="note" type="xs:string"/> </xs:schema>'

schema = xmlschema.XMLSchema(my_xsd)
data = json.dumps({'note': 'this is a Note text'})

xml = xmlschema.from_json(data, schema=schema, preserve_root=True)

ElementTree(xml).write('my_xml.xml')

对于更复杂的 xsd,我更喜欢使用 generateDS,它甚至可以为非常大的 xsd 文件创建非常非常可靠的类.

for more complex xsd's I prefer working with generateDS, which creates very very solid classes from even for very large xsd files.

这篇关于使用 python 从 XSD 文件创建特定的 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆