xsdata.formats.dataclass.parsers.nodes module

class xsdata.formats.dataclass.parsers.nodes.XmlNode(position)[source]

Bases: object

A generic interface for xml nodes that need to implement the two public methods to be used in an event based parser with start/end element events. The parser needs to maintain a queue for these nodes and a list of objects that these nodes return.

Parameters

position (int) – The current objects size, when the node is created.

position: int
next_node(element, position, ctx)[source]

Initialize the next 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
Return type

XmlNode

parse_element(element, 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

Tuple[Optional[QName], Any]

Returns

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

Parameters
  • element (Element) –

  • objects (List[Any]) –

class xsdata.formats.dataclass.parsers.nodes.ElementNode(position, meta, config)[source]

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

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

Parameters
  • meta (XmlMeta) – xml metadata of a dataclass model.

  • config (ParserConfig) – Parser config instance passed down from the root node.

  • position (int) –

meta: XmlMeta
config: ParserConfig
parse_element(element, objects)[source]

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

Return type

Tuple[Optional[QName], Any]

Returns

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

Parameters
  • element (Element) –

  • objects (List[Any]) –

next_node(element, position, ctx)[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

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

Parameters
class xsdata.formats.dataclass.parsers.nodes.RootNode(position, meta, config)[source]

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

Parameters
next_node(element, position, ctx)[source]

Override parent to return itself if the current element is root.

Parameters
Return type

XmlNode

meta
config
class xsdata.formats.dataclass.parsers.nodes.WildcardNode(position, var)[source]

Bases: xsdata.formats.dataclass.parsers.nodes.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) – xml var instance

  • position (int) –

var: XmlVar
parse_element(element, 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

Tuple[Optional[QName], Any]

Returns

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

Parameters
  • element (Element) –

  • objects (List[Any]) –

next_node(element, position, ctx)[source]

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

Notes: Wildcard nodes can only queue other wildcard nodes.

Parameters
Return type

XmlNode

class xsdata.formats.dataclass.parsers.nodes.UnionNode(position, var, ctx)[source]

Bases: xsdata.formats.dataclass.parsers.nodes.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
ctx: XmlContext
next_node(element, position, ctx)[source]

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

Parameters
Return type

XmlNode

parse_element(element, 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

Tuple[Optional[QName], Any]

Returns

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

Parameters
  • element (Element) –

  • objects (List[Any]) –

parse_class(element, clazz)[source]

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

Parameters
  • element (Element) –

  • clazz (Type[~T]) –

Return type

Optional[~T]

classmethod score_object(obj)[source]

Sum all not None field values for the given object.

Parameters

obj (Any) –

Return type

int

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

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

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

Parameters
  • var (XmlVar) – xml var instance

  • position (int) –

var: XmlVar
parse_element(element, objects)[source]

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

Return type

Tuple[Optional[QName], Any]

Returns

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

Parameters
  • element (Element) –

  • objects (List) –

next_node(element, position, ctx)[source]

Initialize the next 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
Return type

XmlNode

class xsdata.formats.dataclass.parsers.nodes.SkipNode(position)[source]

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

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

Parameters

position (int) –

next_node(element, position, ctx)[source]

Skip the current child.

Parameters
Return type

XmlNode

parse_element(element, objects)[source]

Skip parsing the current element.

Parameters
  • element (Element) –

  • objects (List[Any]) –

Return type

Tuple[Optional[QName], Any]

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

Bases: object

Xml parsing and binding for dataclasses.

Parameters
  • config (ParserConfig) – Parser configuration

  • context (XmlContext) – Model metadata builder

  • namespaces – Store the prefix/namespace as they are parsed.

config: ParserConfig
context: XmlContext
namespaces: Namespaces
parse(source, clazz)[source]
Parameters
  • source (Union[_Element, _ElementTree]) –

  • clazz (Type[~T]) –

Return type

~T

parse_context(context, clazz)[source]

Dispatch elements to handlers as they arrive and are fully parsed.

Raises

ParserError – When the requested type doesn’t match the result object

Parameters
Return type

~T

add_namespace(namespace)[source]

Add the given namespace in the registry.

Parameters

namespace (Tuple) –

queue(element, queue, objects)[source]

Queue the next xml node for parsing based on the given element qualified name.

Parameters
dequeue(element, queue, objects)[source]

Use the last xml node to parse the given element and bind any child objects.

Return type

Any

Returns

Any: A dataclass instance or a python primitive value or None

Parameters