I'm using xsdata in a new project and noticed that XML is successfully parsed regardless of the root element name. If this is a feature and not a bug, is there an option to tell the parser to fail if the root element name doesn't match the dataclass (or Meta) name?
from dataclasses import dataclass
from xsdata.formats.dataclass.parsers import XmlParser
from xsdata.formats.dataclass.serializers import XmlSerializer
@dataclass
class Person:
first_name: str
last_name: str
obj = Person(first_name="Bob", last_name="Smith")
serializer = XmlSerializer()
print(serializer.render(obj))
This works as expected.
parser = XmlParser()
p = parser.from_string("<Person><first_name>Bob</first_name><last_name>Smith</last_name></Person>", Person)
print(p.first_name)
This does too, which surprised me.
parser = XmlParser()
p = parser.from_string("<Foo><first_name>Bob</first_name><last_name>Smith</last_name></Foo>", Person)
print(p.first_name)
I'm using xsdata in a new project and noticed that XML is successfully parsed regardless of the root element name. If this is a feature and not a bug, is there an option to tell the parser to fail if the root element name doesn't match the dataclass (or Meta) name?
This works as expected.
This does too, which surprised me.