简体   繁体   English

如何解析 xsd 并使用 python 从中创建 XML?

[英]How to parse xsd and create XML's from it with python?

I'm very begginer with python and I have task to parse xsd schema to python code, fill nodes with values and create final XML file from it. I'm very begginer with python and I have task to parse xsd schema to python code, fill nodes with values and create final XML file from it. Is it possible with generateDS tool?可以使用 generateDS 工具吗? I can't find clear anwser in documentation.我在文档中找不到明确的答案。 Maybe theres another way to do it?也许还有另一种方法可以做到这一点? Thanks for anwsers感谢回答者

This question is rather broad to answer in a single post.这个问题相当广泛,可以在一篇文章中回答。 So I will give the correct method that you would need to follow to solve the problem.因此,我将给出解决问题所需遵循的正确方法。

Firstly, XSD is the schema definition of an XML instance, essentially the rules of the XML file.首先,XSD 是 XML 实例的模式定义,本质上是 XML 文件的规则。 Parsing is not the right word to use here, instead creating an XML instance from a XSD would be a better sound.解析不是在这里使用的正确词,而是从 XSD 创建一个 XML 实例会更好。

You would need to use pyxb to process your xml schema definitions.您需要使用pyxb来处理您的 xml 模式定义。 See this , its a good walkthrough.看到这个,它是一个很好的演练。 You can also use this library to do the same job.你也可以使用这个库来做同样的工作。

Now that your definitions are processed you can then insert/delete values from your XML instance.现在您的定义已处理完毕,您可以从 XML 实例中插入/删除值。 See this for an example.请参阅示例。

Maybe someone in future will have same problem as I had, so there's a tip: to create objects from Schema use generateDS tool, it's a way easier to use rather than pyxb也许将来有人会遇到和我一样的问题,所以有一个提示:使用generateDS工具从 Schema 创建对象,这是一种比pyxb更容易使用的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM