-
-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Iterate over content / child elements #261
Comments
No there is no such helper, but it's not very difficult to write one, here something similar that I wrote in order to fetch all the obj children that matched a base class. xsdata/xsdata/models/mixins.py Lines 190 to 204 in 026ef55
|
Agreed, after digging into dataclass deeper, i came to a similair solution, thanks for providing the info! However, after further investigation, it looks like the order of the elements yielded by that is not in document order Since the order in our XML docs are very relevant in most cases, this is a no-go for us. Should i raise a new issue for this? |
Oh I see what you need. Cool that's part of the serializer logic I could move it to a public helper, in pr #247 xsdata/xsdata/formats/dataclass/serializers/xml.py Lines 214 to 248 in 623ee6d
|
Hey man take a look at In [1]: from xsdata.formats.dataclass.serializers import XmlSerializer
In [2]: from xsdata.formats.dataclass.context import XmlContext
In [3]: from tests.fixtures.books.fixtures import books
In [4]: books.book[0]
Out[4]: BookForm(author='Hightower, Kim', title='The First Book', genre='Fiction', price=44.95, pub_date='2000-10-01', review='An amazing story of nothing.', id='bk001', lang='en')
In [7]: context = XmlContext()
In [8]: meta = context.fetch(books.__class__)
In [9]: for var, value in XmlSerializer.next_value(books, meta):
...: print(value)
...:
[BookForm(author='Hightower, Kim', title='The First Book', genre='Fiction', price=44.95, pub_date='2000-10-01', review='An amazing story of nothing.', id='bk001', lang='en'), BookForm(author='Nagata, Suanne', title='Becoming Somebody', genre='Biography', price=33.95, pub_date='2001-01-10', review='A masterpiece of the fine art of gossiping.', id='bk002', lang='en')] In the generator you can do a check for list instance and create a recursive method, the var holds teh attribute metadata used for binding purposes you can ignore it. The values will be in the order the fields are defined with support for sequential elements as well. |
Thank you. i will have a look! |
In an xsdata representation created from a XSD schema from an XML document like the following
is there a uniform way to iterate over the nested elements, maybe something like
?
In my current TMessageDb class created by xsdata, there is an attribute "modules" of kind list, but
the child elements are stored in different attributes depending on the class, so here TDataTypes would have an list element "packettype".
Regards
The text was updated successfully, but these errors were encountered: