UserXmlParser#

class xsdata.formats.dataclass.parsers.UserXmlParser(config=<factory>, context=<factory>, handler=<class 'xsdata.formats.dataclass.parsers.handlers.native.XmlEventHandler'>)[source]#

Xml parser for dataclasses with hooks to events.

The event hooks allow custom parsers to inject custom logic between the start/end element events.

Parameters:
ns_map#

The parsed namespace prefix-URI map

hooks_cache#

The hooks cache is used to avoid inspecting the class for custom methods on duplicate events.

handler#

alias of XmlEventHandler

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

Build and queue the XmlNode for the starting element.

Override to emit the start element event.

Parameters:
  • clazz (Optional[Type]) – The target class type, auto locate if omitted

  • queue (List[XmlNode]) – The XmlNode queue list

  • objects (List[Tuple[Optional[str], Any]]) – The list of all intermediate parsed objects

  • qname (str) – The element qualified name

  • attrs (Dict) – The element attributes

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

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

Parse the last xml node and bind any intermediate objects.

Override to emit the end element event if the binding process is successful.

Parameters:
  • queue (List[XmlNode]) – The XmlNode queue list

  • objects (List[Tuple[Optional[str], Any]]) – The list of all intermediate parsed objects

  • qname (str) – The element qualified name

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

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

Return type:

bool

Returns:

Whether the binding process was successful.

emit_event(event, name, **kwargs)[source]#

Propagate event to subclasses.

Match event and name to a subclass method and trigger it with any input keyword arguments.

Example::

event=start, name={urn}bookTitle -> start_booking_title(**kwargs)

Parameters:
  • event (str) – The event type start|end

  • name (str) – The qualified name of the element

  • kwargs (Any) – Additional keyword arguments passed to the hooks

__eq__(other)#

Return self==value.

__hash__ = None#
from_bytes(source, clazz=None)#

Parse the input source bytes object into the target class type.

If no clazz is provided, the binding context will try to locate it from imported dataclasses.

Parameters:
  • source (bytes) – The source bytes object to parse

  • clazz (Optional[Type[TypeVar(T)]]) – The target class type to parse the source bytes object

Return type:

TypeVar(T)

Returns:

An instance of the specified class representing the parsed content.

from_path(path, clazz=None)#

Parse the input file into the target class type.

If no clazz is provided, the binding context will try to locate it from imported dataclasses.

Parameters:
  • path (Path) – The path to the input file

  • clazz (Optional[Type[TypeVar(T)]]) – The target class type to parse the file into

Return type:

TypeVar(T)

Returns:

An instance of the specified class representing the parsed content.

from_string(source, clazz=None)#

Parse the input source string into the target class type.

If no clazz is provided, the binding context will try to locate it from imported dataclasses.

Parameters:
  • source (str) – The source string to parse

  • clazz (Optional[Type[TypeVar(T)]]) – The target class type to parse the source string into

Return type:

TypeVar(T)

Returns:

An instance of the specified class representing the parsed content.

parse(source, clazz=None)#

Parse the input file or stream into the target class type.

If no clazz is provided, the binding context will try to locate it from imported dataclasses.

Parameters:
  • source (Any) – The source file or stream object to parse

  • clazz (Optional[Type[TypeVar(T)]]) – The target class type to parse the source bytes object

Return type:

TypeVar(T)

Returns:

An instance of the specified class representing the parsed content.

register_namespace(prefix, uri)#

Register the uri prefix in the namespace registry.

Parameters:
  • prefix (Optional[str]) – Namespace prefix

  • uri (str) – Namespace uri