sissaschool / xmlschema

XML Schema validator and data conversion library for Python
MIT License
411 stars 74 forks source link

xsd_schema to_dict only read n amount of data #376

Closed alonahmias closed 7 months ago

alonahmias commented 9 months ago

I have the following code: xsd_schema = XMLSCHEMA11(await schema_file.read()) xml_dict = xsd_schema.to_dict(await data_file.read()) and i want to convert the data file to dict but only for n amount of data. i have files with milions of objects and i want to read only the first n amount of data, is it possible?

brunato commented 9 months ago

Currently no option is available for doing this, but in 3.0 develop I've already included a callable option (stop_validation) for stopping or skipping validation on certain custamizable condition.

I'll check if a similar option is applicable on decoding (maybe to merge in a single option).

brunato commented 9 months ago

Hi, in new major release the new option validation_hook can be used also for solving your issue, e.g.:

tags_num = 0

def stop_decoding(_e, _xsd_element):
    nonlocal tags_num
    if tags_num >= 10:
        return True
    tags_num += 1
    return False

obj = schema.decode(xml_file, validation_hook=stop_decoding)