XML De/serialization

The code that de/serializes AAS models from and to XML documents lives in the module aas_core3.xmlization.

Serialize

You serialize the AAS model to XML-encoded text by calling the function aas_core3.xmlization.to_str().

If you want the same text to be written incrementally to a typing.TextIO stream, you can use the function aas_core3.xmlization.write().

Here is an example snippet:

import aas_core3.types as aas_types
import aas_core3.xmlization as aas_xmlization

# Prepare the environment
environment = aas_types.Environment(
    submodels=[
        aas_types.Submodel(
            id="some-unique-global-identifier",
            submodel_elements=[
                aas_types.Property(
                    id_short = "some_property",
                    value_type=aas_types.DataTypeDefXSD.INT,
                    value="1984"
                )
            ]
        )
    ]
)

# Serialize to an XML-encoded string
text = aas_xmlization.to_str(environment)

print(text)

Expected output:

<environment xmlns="https://admin-shell.io/aas/3/0"><submodels><submodel><id>some-unique-global-identifier</id><submodelElements><property><idShort>some_property</idShort><valueType>xs:int</valueType><value>1984</value></property></submodelElements></submodel></submodels></environment>

De-serialize

You can de-serialize an environment from XML coming from four different sources by using different functions:

Here is a snippet which parses XML as text and then de-serializes it into an instance of Environment:

import aas_core3.xmlization as aas_xmlization

text = (
    "<environment xmlns=\"https://admin-shell.io/aas/3/0\">" +
    "<submodels><submodel>" +
    "<id>some-unique-global-identifier</id>" +
    "<submodelElements><property><idShort>someProperty</idShort>" +
    "<valueType>xs:boolean</valueType></property></submodelElements>" +
    "</submodel></submodels></environment>"
)

environment = aas_xmlization.environment_from_str(text)

for something in environment.descend():
    print(type(something))

Expected output:

<class 'aas_core3.types.Submodel'>
<class 'aas_core3.types.Property'>

Errors

If the XML document comes in an unexpected form, our SDK throws a aas_core3.xmlization.DeserializationException. This can happen, for example, if unexpected XML elements or XML attributes are encountered, or an expected XML element is missing.