xsdata.formats.dataclass.parsers.nodes module

class xsdata.formats.dataclass.parsers.nodes.ElementNode(meta, attrs, ns_map, config, context, position, mixed=False, derived=False)[source]

Bases: xsdata.formats.dataclass.parsers.mixins.XmlNode

Element type node is equivalent to xml elements and is used to bind user defined dataclasses.

Parameters
  • meta (XmlMeta) – Model xml metadata

  • attrs (Dict) – Key-value attribute mapping

  • ns_map (Dict) – Prefix-URI Namespace mapping

  • config (ParserConfig) – Parser configuration

  • context (XmlContext) – Model xml metadata builder

  • position (int) – The node position of objects cache

  • derived (bool) – The xml element is derived from a base type

  • mixed (bool) –

meta: xsdata.formats.dataclass.models.elements.XmlMeta
attrs: Dict
ns_map: Dict
config: xsdata.formats.dataclass.parsers.config.ParserConfig
context: xsdata.formats.dataclass.context.XmlContext
position: int
mixed: bool = False
derived: bool = False
FIND_MODES_ORDERED: ClassVar[Iterable[xsdata.formats.dataclass.models.elements.FindMode]] = (<FindMode.NOT_WILDCARD: 6>, <FindMode.WILDCARD: 5>)
bind(qname, text, tail, objects)[source]

Parse the given element attributes/text, find all child objects and mixed content and initialize a new dataclass instance.

Return type

bool

Returns

A tuple of the object’s qualified name and the new object.

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

Initialize the next node to be queued for the given starting element.

Search by the given element tag for a matching variable and create the next node by the variable type.

Return type

XmlNode

Returns

The next node to be queued.

Raises

ParserError if the element is unknown and parser config is strict.

Parameters
  • qname (str) –

  • attrs (Dict) –

  • ns_map (Dict) –

  • position (int) –

build_node(var, attrs, ns_map, position)[source]
Parameters
Return type

Optional[XmlNode]

fetch_vars(qname)[source]
Parameters

qname (str) –

Return type

Iterator[XmlVar]

build_element_node(clazz, attrs, ns_map, position, derived)[source]
Parameters
Return type

Optional[XmlNode]

class xsdata.formats.dataclass.parsers.nodes.AnyTypeNode(var, attrs, ns_map, position, mixed=False)[source]

Bases: xsdata.formats.dataclass.parsers.mixins.XmlNode

Parameters
var: xsdata.formats.dataclass.models.elements.XmlVar
attrs: Dict
ns_map: Dict
position: int
mixed: bool = False
has_children: bool = False
child(qname, attrs, ns_map, position)[source]

Initialize the next child node to be queued, when a new xml element starts.

This entry point is responsible to create the next node type with all the necessary information on how to bind the incoming input data.

Parameters
  • qname (str) –

  • attrs (Dict) –

  • ns_map (Dict) –

  • position (int) –

Return type

XmlNode

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

Parse the current element bind child objects and return the result.

This entry point is called when an xml element ends and is responsible to parse the current element attributes/text, bind any children objects and initialize a new object.

Return type

bool

Returns

Whether or not anything was appended in the objects list.

Parameters
class xsdata.formats.dataclass.parsers.nodes.WildcardNode(var, attrs, ns_map, position)[source]

Bases: xsdata.formats.dataclass.parsers.mixins.XmlNode

Wildcard nodes are used for extensible elements that can hold any attribute and content and don’t have a specific dataclass or primitive type.

Notes:

In the future this node should check all known user defined models in the target namespace and use that instead of the generic.

Parameters
  • var (XmlVar) – Class field xml var instance

  • attrs (Dict) – Key-value attribute mapping

  • ns_map (Dict) – Prefix-URI Namespace mapping

  • position (int) – The node position of objects cache

var: xsdata.formats.dataclass.models.elements.XmlVar
attrs: Dict
ns_map: Dict
position: int
bind(qname, text, tail, objects)[source]

Parse the given element attributes/text/tail, find all child objects and mixed content and initialize a new generic element instance.

Return type

bool

Returns

A tuple of the object’s qualified name and a new xsdata.formats.dataclass.models.generics.AnyElement instance.

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

Initialize the next wildcard node to be queued for the given starting element.

Parameters
  • qname (str) –

  • attrs (Dict) –

  • ns_map (Dict) –

  • position (int) –

Return type

XmlNode

class xsdata.formats.dataclass.parsers.nodes.UnionNode(var, attrs, ns_map, position, context, level=<factory>, events=<factory>)[source]

Bases: xsdata.formats.dataclass.parsers.mixins.XmlNode

Union nodes are used for variables with more than one possible types where at least one of them is a dataclass.

Parameters
  • var (XmlVar) – Class field xml var instance

  • attrs (Dict) – Key-value attribute mapping

  • ns_map (Dict) – Prefix-URI Namespace mapping

  • position (int) – The node position of objects cache

  • context (XmlContext) – Model xml context cache

  • level (int) – Current node level

  • events (List) – Record node events

var: xsdata.formats.dataclass.models.elements.XmlVar
attrs: Dict
ns_map: Dict
position: int
context: xsdata.formats.dataclass.context.XmlContext
level: int
events: List
child(qname, attrs, ns_map, position)[source]

Skip all child nodes as we are going to parse the complete element tree.

Parameters
  • qname (str) –

  • attrs (Dict) –

  • ns_map (Dict) –

  • position (int) –

Return type

XmlNode

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

The handler will make multiple tries to bind the given element to one of the available dataclass var types convert it to one of the available primitive types.

The first shoe that fits wins!

Raises

ParserError – When all attempts fail

Return type

bool

Returns

A tuple of the object’s qualified name and the new object.

Parameters
parse_class(clazz)[source]

Initialize a new XmlParser and try to parse the given element.

Parameters

clazz (Type[~T]) –

Return type

Optional[~T]

classmethod score_object(obj)[source]

Score a dataclass instance by the field values types.

Weights:
  1. None: 0

  2. str: 1

  3. *: 1.5

Parameters

obj (Any) –

Return type

float

class xsdata.formats.dataclass.parsers.nodes.PrimitiveNode(var, ns_map)[source]

Bases: xsdata.formats.dataclass.parsers.mixins.XmlNode

XmlNode for text elements with primitive values eg str, int, float.

Parameters
  • var (XmlVar) – Class field xml var instance

  • ns_map (Dict) – Prefix-URI Namespace mapping

var: xsdata.formats.dataclass.models.elements.XmlVar
ns_map: Dict
bind(qname, text, tail, objects)[source]

Parse the given element text according to the node possible types.

Return type

bool

Returns

A tuple of the object’s qualified name and the new object.

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

Initialize the next child node to be queued, when a new xml element starts.

This entry point is responsible to create the next node type with all the necessary information on how to bind the incoming input data.

Parameters
  • qname (str) –

  • attrs (Dict) –

  • ns_map (Dict) –

  • position (int) –

Return type

XmlNode

class xsdata.formats.dataclass.parsers.nodes.SkipNode[source]

Bases: xsdata.formats.dataclass.parsers.mixins.XmlNode

The skip node should be used when we want to skip parsing child elements.

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

Skip the current child.

Parameters
  • qname (str) –

  • attrs (Dict) –

  • ns_map (Dict) –

  • position (int) –

Return type

XmlNode

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

Skip parsing the current element.

Parameters
Return type

bool

class xsdata.formats.dataclass.parsers.nodes.NodeParser(config=<factory>, context=<factory>, handler=<class 'xsdata.formats.dataclass.parsers.mixins.EventsHandler'>)[source]

Bases: xsdata.formats.dataclass.parsers.mixins.PushParser

Bind xml nodes to dataclasses.

Parameters
  • config (ParserConfig) – Parser configuration

  • context (XmlContext) – Model metadata builder

  • handler (Type[XmlHandler]) – Override default XmlHandler

  • ms_map – Namespace registry for prefix-URI mappings

config: xsdata.formats.dataclass.parsers.config.ParserConfig
context: xsdata.formats.dataclass.context.XmlContext
handler

alias of xsdata.formats.dataclass.parsers.mixins.EventsHandler

ns_map: Dict
parse(source, clazz)[source]

Parse the XML input stream and return the resulting object tree.

Parameters
  • source (Any) –

  • clazz (Type[~T]) –

Return type

~T

start(clazz, queue, objects, qname, attrs, ns_map)[source]

Queue the next xml node for parsing.

Parameters
end(queue, objects, qname, text, tail)[source]

Parse the last xml node and bind any intermediate objects.

Return type

Any

Returns

The result of the binding process.

Parameters
start_prefix_mapping(prefix, uri)[source]

Add the given prefix-URI namespaces mapping if the prefix is new.

Parameters
class xsdata.formats.dataclass.parsers.nodes.RecordParser(config=<factory>, context=<factory>, handler=<class 'xsdata.formats.dataclass.parsers.mixins.EventsHandler'>, events=<factory>)[source]

Bases: xsdata.formats.dataclass.parsers.nodes.NodeParser

Bind xml nodes to dataclasses with an events recorder.

Parameters
events: List
start(clazz, queue, objects, qname, attrs, ns_map)[source]

Queue the next xml node for parsing.

Parameters
end(queue, objects, qname, text, tail)[source]

Parse the last xml node and bind any intermediate objects.

Return type

Any

Returns

The result of the binding process.

Parameters
start_prefix_mapping(prefix, uri)[source]

Add the given prefix-URI namespaces mapping if the prefix is new.

Parameters