PyXB 的端到端示例.从 XSD 模式到 XML 文档 [英] End-to-end example with PyXB. From an XSD schema to an XML document

查看:25
本文介绍了PyXB 的端到端示例.从 XSD 模式到 XML 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难开始使用 PyXB.

假设我有一个 XSD 文件(一个 XML 模式).我想:

  1. 使用 PyXB 根据架构定义 Python 对象.
  2. 将这些对象作为满足架构的 XML 文件保存到磁盘.

我怎样才能用 PyXB 做到这一点?下面是一个对地址进行编码的 XSD 文件(来自维基百科)的简单示例,但我很难时间甚至开始.

更新

一旦我跑了

pyxbgen -u 示例.xsd -m 示例

我得到一个具有以下类的 example.py:

example.Address example.STD_ANONexample.CTD_ANON example.StringIOexample.CreateFromDOM example.pyxbexample.CreateFromDocument example.sys示例.命名空间

我想我了解 CreateFromDocument 的作用 - 它大概读取 XML 并创建相应的 python 对象 - 但是我使用哪个类来创建新对象然后将其保存到XML?

解决方案

一个简单的谷歌搜索带来了这个:http://pyxb.sourceforge.net/userref_pyxbgen.html#pyxbgen

特别是说:

<块引用>

使用以下命令将其翻译成 Python:

pyxbgen -u po1.xsd -m po1

<块引用>

-u 参数标识架构描述命名空间内容的文档.该参数可能是一个本地系统文件的路径,或网络可访问的 URL位置像http://www.weather.gov/forecasts/xml/DWMLgen/schema/DWML.xsd.他们参数指定 Python 模块所使用的名称为前面架构中的命名空间生成的绑定.运行此命令后,Python 绑定将位于名为的文件中po1.py.

编辑更新后:

既然您已经生成了 Address 类和所有关联的帮助程序,请查看 http://pyxb.sourceforge.net/userref_usebind.html 以了解如何使用它们.对于您的具体问题,您想研究在 Python 代码中创建实例"段落.基本上要从您的应用程序数据生成 XML,您只需执行以下操作:

导入示例地址 = 地址()address.FullName = "Jo La Banane"# 填写其他成员地址# ...使用 open('myoutput.xml', 'w') 作为文件f.write(address.toxml("utf-8"))

现在由你来好奇并阅读正在生成的代码,pyxb 的文档,调用各种生成的方法并进行实验!

I am having a hard time getting started with PyXB.

Say I have an XSD file (an XML schema). I would like to:

  1. Use PyXB to define Python objects according to the schema.
  2. Save those objects to disk as XML files that satisfy the schema.

How can I do this with PyXB? Below is a simple example of an XSD file (from Wikipedia) that encodes an address, but I am having a hard time even getting started.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Address">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="FullName" type="xs:string" />
        <xs:element name="House" type="xs:string" />
        <xs:element name="Street" type="xs:string" />
        <xs:element name="Town" type="xs:string" />
        <xs:element name="County" type="xs:string" minOccurs="0" />
        <xs:element name="PostCode" type="xs:string" />
        <xs:element name="Country" minOccurs="0">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="IN" />
              <xs:enumeration value="DE" />
              <xs:enumeration value="ES" />
              <xs:enumeration value="UK" />
              <xs:enumeration value="US" />
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Update

Once I run

pyxbgen -u example.xsd -m example

I get a example.py that has the following classes:

example.Address             example.STD_ANON
example.CTD_ANON            example.StringIO
example.CreateFromDOM       example.pyxb
example.CreateFromDocument  example.sys
example.Namespace           

I think I understand what CreateFromDocument does - it presumably reads an XML and creates the corresponding python object-, but which class do I use to create a new object and then save it to an XML?

解决方案

A simple google search brings this: http://pyxb.sourceforge.net/userref_pyxbgen.html#pyxbgen

In particular the part that says:

Translate this into Python with the following command:

pyxbgen -u po1.xsd -m po1

The -u parameter identifies a schema document describing contents of a namespace. The parameter may be a path to a file on the local system, or a URL to a network-accessible location like http://www.weather.gov/forecasts/xml/DWMLgen/schema/DWML.xsd. The -m parameter specifies the name to be used by the Python module holding the bindings generated for the namespace in the preceding schema. After running this, the Python bindings will be in a file named po1.py.

EDIT Following your update:

Now that you have your generated Address class and all the associated helpers, look at http://pyxb.sourceforge.net/userref_usebind.html in order to learn how to use them. For your specific question, you want to study the "Creating Instances in Python Code" paragraph. Basically to generate XML from your application data you simply do:

import example
address = Address()
address.FullName = "Jo La Banane"
# fill other members of address
# ...
with open('myoutput.xml', 'w') as file
    f.write(address.toxml("utf-8"))

Now it's up to you to be curious and read the code being generated, pyxb's doc, call the various generated methods and experiment!

这篇关于PyXB 的端到端示例.从 XSD 模式到 XML 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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