Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if node.text is None:
if use_defaults:
text = xsd_element.default or ''
else:
text = ''
else:
text = node.text.strip() if list(node) else node.text
if spaces_for_tab is not None:
text = text.replace('\t', ' ' * spaces_for_tab)
try:
value = xsd_element.decode(text)
except XMLSchemaValidationError as err:
value = err.value
errors.append(
XMLSchemaValidationError(xsd_element, err.value, err.reason, node, xsd_element.elem)
)
except XMLSchemaDecodeError as err:
value = text
errors.append(
XMLSchemaDecodeError(xsd_element, err.text, err.decoder, err.reason, node, xsd_element.elem)
)
if node_dict:
# if we have a dictionary add the text as a dictionary value (if there is any)
if len(text) > 0:
node_dict['_text'] = value
else:
# if we don't have child nodes or attributes, just set the text
node_dict = value
return node_dict
Decodes XML data using the XSD component.
:param data: the object containing the XML data. Can be a string for an \
attribute or a simple type definition, or an ElementTree's Element for \
other XSD components.
:param args: arguments that maybe passed to :func:`XMLSchema.iter_decode`.
:param kwargs: keyword arguments from the ones included in the optional \
arguments of the :func:`XMLSchema.iter_decode`.
:return: A dictionary like object if the XSD component is an element, a \
group or a complex type; a list if the XSD component is an attribute group; \
a simple data type object otherwise.
:raises: :exc:`XMLSchemaValidationError` if the object is not decodable by \
the XSD component, or also if it's invalid when ``validate=True`` is provided.
"""
for chunk in self.iter_decode(data, *args, **kwargs):
if isinstance(chunk, XMLSchemaValidationError):
raise chunk
return chunk
to_dict = decode
if isinstance(new_item, list):
node_dict[child.tag] = [node_item, new_item]
else:
node_dict[child.tag] = [node_item, [new_item]]
elif isinstance(new_item, list):
node_dict[child.tag].append(new_item)
else:
node_dict[child.tag].append([new_item])
tail = child.tail.strip()
if tail:
try:
xsd_element.validate(tail)
except XMLSchemaValidationError as err:
errors.append(
XMLSchemaValidationError(xsd_element, err.value, err.reason, xsd_element.elem, node)
)
# Add the element's content
if node.text is None:
if use_defaults:
text = xsd_element.default or ''
else:
text = ''
else:
text = node.text.strip() if list(node) else node.text
if spaces_for_tab is not None:
text = text.replace('\t', ' ' * spaces_for_tab)
try:
value = xsd_element.decode(text)
except XMLSchemaValidationError as err:
xsd_attribute = xsd_element.get_attribute(name)
except KeyError as err:
errors.append(XMLSchemaValidationError(
validator=xsd_element,
value=value_string,
reason="attribute %s not in the schema" % err,
elem=node,
schema_elem=xsd_element.elem
))
continue
try:
attr_dict[name] = xsd_attribute.decode(value_string)
except XMLSchemaValidationError as err:
attr_dict[name] = err.value
errors.append(XMLSchemaValidationError(
err.validator, err.value, err.reason, node, xsd_attribute.elem
))
except XMLSchemaDecodeError as err:
attr_dict[name] = value_string
errors.append(err)
if use_defaults:
# Set defaults for missing schema's attributes
for name in list(set(schema.get_attributes(node_path)) - set(node.keys())):
default_value = schema.get_attribute(name, node_path).default
if default_value is not None:
attr_dict[name] = schema.get_attribute(name, node_path).decode(default_value)
node_dict.update(attr_dict)
# Adds the subelements recursively
for child in node:
def _element_to_dict(node, node_path):
node_dict = dict_class()
xsd_element = schema.get_element(node_path)
if node.attrib:
# if we have attributes, decode them
attr_dict = dict_class()
for name, value_string in node.attrib.items():
try:
try:
xsd_attribute = schema.get_attribute(name, node_path)
except KeyError:
xsd_attribute = xsd_element.get_attribute(name)
except KeyError as err:
errors.append(XMLSchemaValidationError(
validator=xsd_element,
value=value_string,
reason="attribute %s not in the schema" % err,
elem=node,
schema_elem=xsd_element.elem
))
continue
try:
attr_dict[name] = xsd_attribute.decode(value_string)
except XMLSchemaValidationError as err:
attr_dict[name] = err.value
errors.append(XMLSchemaValidationError(
err.validator, err.value, err.reason, node, xsd_attribute.elem
))
except XMLSchemaDecodeError as err: