Skip to content

Is it possible to specify the namespace prefixes and the default namespace #922

Answered by AlexandrosPlessias
couling asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @couling,

You can use below codesnap:

from xsdata.formats.dataclass.serializers.xml import XmlSerializer
from xsdata.formats.dataclass.serializers.config import SerializerConfig

serializer_config: SerializerConfig = SerializerConfig(pretty_print=True)
xml_serializer: XmlSerializer = XmlSerializer(config=serializer_config)

The foo param is the root class of your xml & ns_map can be a dict variable if you want

For
<foo xmlns="http://bar.example.com" />
try below:
xml_foo: str = xml_serializer.render(foo, ns_map={"": "http://bar.example.com"})

For
<bar:foo xmlns:bar="http://bar.example.com" />
try below:
xml_foo: str = xml_serializer.render(foo, ns_map={"": "http://bar.example.com","…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by couling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants