0

我有一个xsd用于pyxb生成对象类的对象。到目前为止,一切正常,我能够接收文档,错误处理等工作正常。我唯一的问题是,我的 xsd 中有以下内容:

<xs:element name="users">
  <xs:complexType>
    <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" ref="user" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

我在user别处定义了复杂类型。有时我想user从主 xml 文档中获取一个,我想从中创建 python 类绑定......但我做不到。pyxb只允许我从整个文档创建(使用CreateFromDocument函数)。反正有没有得到那一个user元素?

我阅读了以下内容:

https://developer.yahoo.com/python/python-xml.html

http://pyxb.sourceforge.net/api/pyxb.binding.basis.element-class.html

http://pyxb.sourceforge.net/userref_pyxbgen.html

4

2 回答 2

1

CreateFromDocument() 将为作为架构中顶级元素的任何 XML 片段创建一个绑定。所以你应该能够做到:

instance = user_xsd.CreateFromDocument(string)

无需先通过 dom 实例。在测试目录中有很多例子可以做到这一点。

于 2014-05-13T00:20:52.923 回答
0

所以答案被隐藏在文档中。

http://pyxb.sourceforge.net/PyXB-1.2.2/examples.html

假设您有一个userxml 文档。你可以做:

from xml.dom import minidom
dom = minidom.parseString(string)

# import the xml file you get from pyxbgen
import user_xsd
user_xsd.CreateFromDOM(dom.documentElement)
于 2014-05-12T23:28:39.827 回答