문제

I have a xsd for which I used pyxb to generate object classes for. So far everything is working, i'm able to take in documents, error handling, etc. is working fine. My only question is this, I have the following in my xsd:

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

I have the user complex type defined elsewhere. Sometimes I want to take a user from the main xml document and I want to create python class bindings from it...but i'm not able to. pyxb only allows me to create from an entire document(using the CreateFromDocument function). Is there anyway to get just that one user element?

I read the following:

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

도움이 되었습니까?

해결책

CreateFromDocument() will create a binding for any XML fragment that is a top-level element in the schema. So you should have been able to do:

instance = user_xsd.CreateFromDocument(string)

without going through a dom instance first. There are a lot of examples that do exactly this in the tests directory.

다른 팁

So the answer was hidden will into the docs.

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

say you have one user xml document. You can do:

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top