Extracting Objects and fields for tags

Contains tips for configurators working with Aware IM
Post Reply
JoshK131
Posts: 47
Joined: Tue Jan 26, 2021 11:09 pm

Extracting Objects and fields for tags

Post by JoshK131 »

Hello.


I have set up a system to allow end users to be able to easily get the correct Tags needed for UDDs.

It uses a python script to extract the Business Objects, Attributes and Reference Attributes from the business space xml file to some csvs.
The csvs can be imported into "Object", "Field", "Relation" BO's.
Zoho_Assist_hetD3unudL.png
Zoho_Assist_hetD3unudL.png (440.49 KiB) Viewed 1563 times
Users can then choose the object and the field to see what the tag should look like.
chrome_XchAVQtUdh.png
chrome_XchAVQtUdh.png (25.87 KiB) Viewed 1563 times

Code: Select all

import xml.etree.ElementTree as ET
import csv

# Load and parse the XML file
tree = ET.parse('input.xml')  # Replace with your XML file path
root = tree.getroot()

# Prepare data for CSV files
objects_data = []
references_data = []
attributes_data = []

for obj_def in root.findall('object_definition'):
    # Data for Objects CSV
    obj_data = {
		'Object.Name': obj_def.get('name'),
		'Object.Tag': obj_def.get('name')
    }
    objects_data.append(obj_data)

    for attr_def in obj_def.findall('attribute_definition'):
        # Check if 'primitive_type' is present
        if 'primitive_type' in attr_def.attrib:
            # Data for Attributes CSV
            label = attr_def.find(".//text_attribute_presentation").get('label') if attr_def.find(".//text_attribute_presentation") is not None else attr_def.get('name')
            attributes_data.append({
		        'Field.ps_Object.Tag': obj_def.get('name'),
		        'Field.Tag': attr_def.get('name'),
		        'Field.Name': label
            })

        # Data for References CSV
        if attr_def.get('multiple') == 'false' and 'reference_type' in attr_def.attrib:
            references_data.append({
		        'Relation.ps_ObjectFrom.Tag': obj_def.get('name'),
		        'Relation.ps_ObjectTo.Tag': attr_def.get('reference_type'),
		        'Relation.Tag': attr_def.get('name'),
		        'Relation.Name': attr_def.get('name').split('_', 1)[1] if '_' in attr_def.get('name') else attr_def.get('name')
            })

# Write to Objects CSV
with open('Objects.csv', 'w', newline='', encoding='utf-8') as file:
    writer = csv.DictWriter(file, fieldnames=objects_data[0].keys())
    writer.writeheader()
    writer.writerows(objects_data)

# Write to References CSV
with open('References.csv', 'w', newline='', encoding='utf-8') as file:
    writer = csv.DictWriter(file, fieldnames=references_data[0].keys())
    writer.writeheader()
    writer.writerows(references_data)

# Write to Attributes CSV
with open('Attributes.csv', 'w', newline='', encoding='utf-8') as file:
    writer = csv.DictWriter(file, fieldnames=attributes_data[0].keys())
    writer.writeheader()
    writer.writerows(attributes_data)

print("CSV files created successfully.")

This whole process can still be optimized, and I may try to automate more of it down the line.
Post Reply