我可以使用xsdata python库(或者您知道的更好的选项)使用xsd将python数据对象(使用dataclass



我的python代码中有一个复杂的数据结构(包括嵌套类和所有类(,使用数据类准确地实现。现在,我需要使用现有的xsd模式定义将带有数据的dataclass实例序列化到xml文件中。我该怎么做?我找到了xsdata,它似乎应该有能力,但我找不到如何做到这一点。

@dataclass
class Address:
address1 : object = field(default=None)
address2 : object = field(default=None)
city : object = field(default=None)
state : object = field(default=None)
pin_code : object = field(default=None)
country : object = field(default=None)

@dataclass
class OrderAddress:
address_name : object = field(default=None)
address_phone : object = field(default=None)
address_details : Address=field(default=None)

以下是相应xsd 的一部分

<xs:complexType name="AddressType">
<xs:annotation>
<xs:documentation>Contains Customer information such as Name, Physical Postal Address, and Contact Information.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="Name" default="Customer" minOccurs="0">
<xs:annotation>
<xs:documentation>(Optional) Name is the full name of the Customer.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="150"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="CustomerAddress1" minOccurs="0">
<xs:annotation>
<xs:documentation>(Optional) CustomerAddress1 is the Address Line 1 of the Customer.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="250"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="CustomerAddress2" minOccurs="0">
<xs:annotation>
<xs:documentation>(Optional) CustomerAddress2 is the Address Line 2 of the Customer.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="250"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="CustomerAddress3" minOccurs="0">
<xs:annotation>
<xs:documentation>(Optional) CustomerAddress3 is the Address Line 3 of the Customer.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="250"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="City" minOccurs="0">
<xs:annotation>
<xs:documentation>(Optional) City is the Customer's City location.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="State" minOccurs="0">
<xs:annotation>
<xs:documentation>(Optional) State is the Customer's State location.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Zip" default="00000" minOccurs="0">
<xs:annotation>
<xs:documentation>(Optional) Zip is the Customer's Zipcode location. If there is no Zipcode provided, if the field is an empty-tag, or if there is no-tag - the System will default to five zero-digits only for billing address.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="20"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Country" minOccurs="0">
<xs:annotation>
<xs:documentation>(Optional) Country is the Customer's Country location.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Phone" minOccurs="0">
<xs:annotation>
<xs:documentation>(Optional) Phone is the Customer's Phone Number.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>

我希望能够从填充了数据的OrderAddress对象输出这个xml,如下所示:

<BillingAddress>
<Name>Name1</Name>
<Address1>Address11</Address1>
<Address2>Address21</Address2>
<Address3>Address31</Address3>
<City>City1</City>
<State>State1</State>
<Zip>Zip1</Zip>
<Country>Country1</Country>
<Email>Email1</Email>
<Phone>Phone1</Phone>
</BillingAddress>

如果您有一个xml模式,您可以使用xsdata的代码生成器为您创建模型。

参见此处

❯ xsdata generate schema.xsd
Parsing schema schema.xsd
Compiling schema schema.xsd
Builder: 1 main and 0 inner classes
Analyzer input: 1 main and 0 inner classes
Analyzer output: 1 main and 0 inner classes
Generating package: init
Generating package: generated.schema

最新更新