}
try:
if date_format in format_map:
datetime.strptime(date_str, format_map[date_format])
else:
print(f"Unsupported date format: {date_format}")
return False
return True
except ValueError:
return False
Function to validate data based on Type Tree constraints
def validate_data(records, constraints):
validation_passed = True
for record in records:
for key, value in record.data.items():
if key in constraints:
print(f"Validating {key}: value={value}, constraints={constraints[key]}") # Debug: Print each field's constraints
if constraints[key]['max_size'] and len(value) > constraints[key]['max_size']:
print(f"Validation Error: '{key}' '{value}' exceeds max length of {constraints[key]['max_size']}")
validation_passed = False
if constraints[key]['date_format']:
if not validate_date_format(value, constraints[key]['date_format']):
print(f"Validation Error: '{key}' '{value}' does not match the format {constraints[key]['date_format']}")
validation_passed = False
return validation_passed
Function to create XML elements from records
def create_xml(records):
root = ET.Element("Records")
for record in records:
record_elem = ET.SubElement(root, "Record")
for key, value in record.data.items():
elem = ET.SubElement(record_elem, key)
elem.text = value
return root
Function to save XML to a file
def save_xml(root, output_file):
tree = ET.ElementTree(root)
tree.write(output_file, encoding="utf-8", xml_declaration=True)
Path to your input file
input_file_path = 'input_file.txt'
Path to your type tree XML
type_tree_file_path = 'dummy_typetree.xml'
Path to output XML file
output_file_path = 'output.xml'
Read and process the input file
dummy_records = read_input_file(input_file_path)
print(f"Dummy Records: {dummy_records}") # Debug: Print the dummy records
Parse the type tree XML for validation
constraints = parse_type_tree(type_tree_file_path)
print(f"Constraints: {constraints}") # Debug: Print the constraints
Validate the data
if validate_data(dummy_records, constraints):
print("Data validation passed.")
# Create XML from records
xml_root = create_xml(dummy_records)
print(f"XML Root: {ET.tostring(xml_root, encoding='unicode')}") # Debug: Print the XML root for verification
# Save XML to file
save_xml(xml_root, output_file_path)
print(f"XML saved to {output_file_path}")
import xml.etree.ElementTree as ET from datetime import datetime
class DummyRecord: def init(self, data): self.data = data
Manually define headers based on your input structure
HEADERS = ['Name', 'DOB', 'Salary']
Function to read and parse the input file
def read_input_file(file_path): records = [] with open(file_path, 'r') as file: for line in file:
Strip whitespace and split by delimiter
Function to parse Type Tree XML and extract constraints
def parse_type_tree(type_tree_file): type_tree = ET.parse(type_tree_file).getroot() constraints = {} for item in type_tree.findall('ITEM'): name = item.get('SimpleTypeName') specifics = item.find('Specifics') max_size = None date_format = None if specifics is not None: size_element = specifics.find('Size') if size_element is not None: max_size = size_element.get('Max') date_format_element = specifics.find('CharDatetimeFormatString') if date_format_element is not None: date_format = date_format_element.text.strip('{}') print(f"Extracted Constraints for {name}: max_size={max_size}, date_format={date_format}") # Debug: Print constraints constraints[name] = {'max_size': int(max_size) if max_size else None, 'date_format': date_format} return constraints
Function to validate date format strictly
def validate_date_format(date_str, date_format): format_map = { 'CCYYMMDD': '%Y%m%d', 'YYMMDD': '%y%m%d', 'MMDDCCYY': '%m%d%Y', 'MMDDYY': '%m%d%y', 'CCYYDDD': '%Y%j', 'YYDDD': '%y%j', 'DDMMCCYY': '%d%m%Y', 'DDMMYY': '%d%m%y', 'DD/MM/CCYY': '%d/%m/%Y', 'MM-DD-YYYY': '%m-%d-%Y'
Add more custom formats as needed
Function to validate data based on Type Tree constraints
def validate_data(records, constraints): validation_passed = True for record in records: for key, value in record.data.items(): if key in constraints: print(f"Validating {key}: value={value}, constraints={constraints[key]}") # Debug: Print each field's constraints if constraints[key]['max_size'] and len(value) > constraints[key]['max_size']: print(f"Validation Error: '{key}' '{value}' exceeds max length of {constraints[key]['max_size']}") validation_passed = False if constraints[key]['date_format']: if not validate_date_format(value, constraints[key]['date_format']): print(f"Validation Error: '{key}' '{value}' does not match the format {constraints[key]['date_format']}") validation_passed = False return validation_passed
Function to create XML elements from records
def create_xml(records): root = ET.Element("Records") for record in records: record_elem = ET.SubElement(root, "Record") for key, value in record.data.items(): elem = ET.SubElement(record_elem, key) elem.text = value return root
Function to save XML to a file
def save_xml(root, output_file): tree = ET.ElementTree(root) tree.write(output_file, encoding="utf-8", xml_declaration=True)
Path to your input file
input_file_path = 'input_file.txt'
Path to your type tree XML
type_tree_file_path = 'dummy_typetree.xml'
Path to output XML file
output_file_path = 'output.xml'
Read and process the input file
dummy_records = read_input_file(input_file_path) print(f"Dummy Records: {dummy_records}") # Debug: Print the dummy records
Parse the type tree XML for validation
constraints = parse_type_tree(type_tree_file_path) print(f"Constraints: {constraints}") # Debug: Print the constraints
Validate the data
if validate_data(dummy_records, constraints): print("Data validation passed.")
else: print("Data validation failed.")