xml tool updates

This commit is contained in:
Karl Hudgell 2024-12-14 22:53:07 +00:00
parent 8205f6ec73
commit 12e920a2c0
2 changed files with 58 additions and 0 deletions

View File

@ -28,3 +28,36 @@ def create_config_xml(folder_path):
output_path = os.path.join(folder_path, "config.xml") output_path = os.path.join(folder_path, "config.xml")
tree.write(output_path, encoding="utf-8", xml_declaration=True) tree.write(output_path, encoding="utf-8", xml_declaration=True)
print(f"Config XML created at: {output_path}") print(f"Config XML created at: {output_path}")
def append_to_config_xml(file_path, uid_list):
# Check if the file exists
file_path = f"{file_path}/config.xml"
if not os.path.exists(file_path):
raise FileNotFoundError(f"The file {file_path} does not exist.")
# Parse the existing XML file
tree = ET.parse(file_path)
root = tree.getroot()
# Find the <list id="maps"> element
maps = root.find(".//list[@id='maps']")
if maps is None:
raise ValueError("The XML file does not contain a <list id='maps'> element.")
# Add new UIDs
for uid in uid_list:
# Check if the UID already exists (escaping 'from' in XPath)
existing = None
for record in maps.findall("record"):
if record.attrib.get('from') == str(uid):
existing = record
break
if existing is None:
# Add the new record if the UID doesn't exist
ET.SubElement(maps, "record", attrib={"from": str(uid), "to": f"graphics/pictures/person/r-{uid}/portrait"})
# Write the updated XML back to the file
tree.write(file_path, encoding="utf-8", xml_declaration=True)
print(f"Appended new UIDs to {file_path}")

25
lib/xml_reader.py Normal file
View File

@ -0,0 +1,25 @@
import xml.etree.ElementTree as ET
def extract_from_values(xml_file):
"""
Extract all 'from' values from the record elements in the XML file.
Args:
xml_file (str): Path to the XML file.
Returns:
list: List of all 'from' attribute values.
"""
from_values = []
# Parse the XML file
tree = ET.parse(xml_file)
root = tree.getroot()
# Find all 'record' elements and extract 'from' attributes
for record in root.findall(".//record"):
from_value = record.get('from')
if from_value:
from_values.append(from_value)
return from_values