You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Digging through the documentation and code, I was unable to figure out if the following was possible, and if so, what the best approach would be. Supposing I had an xml snippet that looked like:
The xml_field_validator used in the custom xml serialization example only really seems capable of mapping one tag to a single attribute. Any suggestions on how to do this, as well as the reverse serialization step would be greatly appreciated.
The text was updated successfully, but these errors were encountered:
I'm not sure if there is a better way to solve this, but I just ended up explicitly modeling A, and then keeping a non-exporting field to hold the nested list:
import pydantic_xml as px
from pydantic_xml import BaseXmlModel
class Row(BaseXmlModel, tag='row'):
items: list[float] = px.element(tag='item')
class Amat(BaseXmlModel, tag='A'):
rows: list[Row] = px.element(tag='row')
class System(BaseXmlModel, tag="system"):
A_data: Amat = px.element(tag='A')
_A: list[list[float]] | None = None
@property
def A(self) -> list[list[float]]:
if self._A is None:
self._A = [[item for item in row.items] for row in self.A_data.rows]
return self._A
@A.setter
def A(self, value: list[list[float]]):
self.A_data = Amat(rows=[Row(items=row) for row in value])
self._A = None
Digging through the documentation and code, I was unable to figure out if the following was possible, and if so, what the best approach would be. Supposing I had an xml snippet that looked like:
I'm trying to map
A
to something like:where
ex = Example.from_xml(doc.xml)
would haveThe
xml_field_validator
used in the custom xml serialization example only really seems capable of mapping one tag to a single attribute. Any suggestions on how to do this, as well as the reverse serialization step would be greatly appreciated.The text was updated successfully, but these errors were encountered: