UnionNode#

class xsdata.formats.dataclass.parsers.nodes.UnionNode(var, attrs, ns_map, position, config, context)[source]#

XmlNode for union fields with at least one data class.

The node will record all child events and in the end will replay them and try to build all possible objects and sort them by score before deciding the winner.

Parameters:
  • var (XmlVar) – The xml var instance

  • attrs (Dict) – The element attributes

  • ns_map (Dict) – The element namespace prefix-URI map

  • position (int) – The current objects length, everything after this position are considered children of this node.

  • config (ParserConfig) – The parser config instance

  • context (XmlContext) – The xml context instance

child(qname, attrs, ns_map, position)[source]#

Record the event for the child element.

This entry point records all events, as it’s not possible to detect the target parsed object type just yet. When this node ends, it will replay all events and attempt to find the best matching type for the parsed object.

Parameters:
  • qname (str) – The element qualified name

  • attrs (Dict) – The element attributes

  • ns_map (Dict) – The element namespace prefix-URI map

  • position (int) – The current length of the intermediate objects

Return type:

XmlNode

bind(qname, text, tail, objects)[source]#

Bind the parsed data into an object for the ending element.

This entry point is called when a xml element ends and is responsible to replay all xml events and parse/bind all the children objects.

Parameters:
  • qname (str) – The element qualified name

  • text (Optional[str]) – The element text content

  • tail (Optional[str]) – The element tail content

  • objects (List) – The list of intermediate parsed objects

Return type:

bool

Returns:

Always returns true, if the binding process fails it raises an exception.

Raises:

ParserError – If none of the candidate types matched the replayed events.

parse_class(clazz)[source]#

Replay the recorded events and attempt to build the target class.

Parameters:

clazz (Type[TypeVar(T)]) – The target class

Return type:

Optional[TypeVar(T)]

Returns:

The target class instance or None if the recorded xml events didn’t fit the class.

parse_value(value, types)[source]#

Parse simple values.

Parameters:
  • value (Any) – The xml value

  • types (List[Type]) – The list of the candidate simple types

Return type:

Any

Returns:

The parsed value or None if value didn’t match with any of the given types.