Simple, fast, lightweight xml parser and validator defined by classes, and useful error messages.
Reason to pick this over other xml parsers:
- Non alias. (Harder to match tag/attribute name with the class variable name)
- Need advanced features. (Like default factory, custom decoder, custom encoder etc.)
TODO: Speed test against pydantic-xml and xmltodict with pydantic.
For the following XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<root value="data" />The following python class can be used to parse it:
class RootAttribute(XmlClass):
value: strIt automatically assumes it is an xml tag attribute.
And then parse it:
root = RootAttribute.from_string(xml_string)For the following XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<root>
<value value="data"/>
</root>The following python class can be used to parse it:
class Value(XmlClass):
value: str
class RootElement(XmlClass):
value: ValueWhen the type is set to be of XmlClass, it will be parsed as a sub element.
For xml text field, it is a bit more complex. For the following XML: """
data1 """The following python class can be used to parse it:
class Value(XmlClass):
data: XmlTextField[str]When the type is set to be of XmlTextField, it will be parsed as a text field, where the text-field is of the type given in the square brackets.
- int
- float
- str
- bool 1
- None
- datetime.datetime 2
- uuid.UUID
- pathlib.Path
- typing.Any 3
- Literal
- Enum
- list 4
- tuple 4
- union
- XmlClass
- XmlTextField
Trailing underscore is ignored when mapping from XML to python class. It is used in the casses where a child element tag and a attribute have the same name. Through not a requirement, the trailing underscore should be used in the attribute name.
Since dash in python name is not allowed, dash in tag or attribute name is replaced with underscore.
- This is a work in progress, ATM it only parses the XML data, to a python class.
- UNSURE: Do None type hint as the only one even make sense?
- UNSURE: Should null value be None type hint, and something else for optional?
- Test
XmlClass | Nonecase, where there is parsing error in XmlClass. - Test
OneTwo: typing.Literal["firstPasser", "secondPasser"] | Nonecase, where it do not Exist. - Test
xmlClass | AnotherXmlClasscase, with different Names.
- Make the error messages better for when there is a error deep inside a xmlClass.
- Make a test for typing.Any.
- Added
defusedxmlas dependency. (Security reasons) - Fix ALL type hints...
- Add support for decoder and encoder.
- Add support for custom parsers. (XmlDataParser[MyParserOfData])
- Make good and useful error messages.
Footnotes
-
Where ["true", "1", "yes", "on"] is true and ["false", "0", "no", "off"] is false. ↩
-
Is parsed by the
datetime.datetime.fromisoformatmethod. ↩ -
Is parsed as is (as string). Only check if there is data. ↩
-
Assume that the children can only be of type: XmlClass. Since multiple attributes are not allowed, and a data separator is not defined. ↩ ↩2