我有以下 xml 架构:
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="DataPackage">
<xsd:sequence>
<xsd:element name="timestamp" type="xsd:float" default="0.0"/>
<xsd:element name="type" type="xsd:string" default="None"/>
<xsd:element name="host" type="xsd:string" default="None"/>
<xsd:element name="data" type="Data" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Data">
<xsd:sequence>
<xsd:element name="item" type="Item" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Item">
<xsd:sequence>
<xsd:element name="key" type="xsd:string"/>
<xsd:element name="val" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
我曾经pyxbgen -u DataPackage.xsd -m DataPackage
生成相应的 python 类并使用它们生成以下 xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<DataPackage>
<timestamp>1378970933.29</timestamp>
<type>None</type>
<host>Client 1</host>
<data>
<item>
<key>KEY1</key>
<val>value1</val>
</item>
</data>
</DataPackage>
如果我尝试在 python 解释器中使用以下内容阅读此内容:
import DataPackage
xml = file("dataPackage-Test.xml").read()
data = DataPackage.CreateFromDocument(xml)
我得到了例外:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "DataPackage.py", line 54, in CreateFromDocument
instance = handler.rootObject()
File "/usr/local/lib/python2.7/dist-packages/pyxb/binding/saxer.py", line 274, in rootObject
raise pyxb.UnrecognizedDOMRootNodeError(self.__rootObject)
pyxb.exceptions_.UnrecognizedDOMRootNodeError: <pyxb.utils.saxdom.Element object at 0x9c7c76c>
有人知道怎么了?