Please don't banish me from the group. This is the solution I settled on
#! Python3
"""
Program uses xml.etree module to select data elements from the xml tree, names
row headers, and csv.writer then writes them to a csv file. Rows are appended
to the file as each file in the directory is processed.,
Program Author: Gerald I Cheves
Date written: 25 June 2018
Last Revision: 28 June 2018
"""
import winsound
import xml.etree.ElementTree as ET
import os
import sys
import csv
with open('C:\\Users\\GCheves\\ aladin\\Output\\xml-csv-output.csv', 'w') as fout:
writer = csv.writer(fout, dialect='excel')
row = ['ObjectType', 'ObjectID', 'Ethnicity', 'Title', 'Address', 'City', 'State', 'Gender', 'DateOfBirth', 'FirstName', 'MiddleName', 'Lastname', 'Height', 'PropertyRole', 'LinkType', 'ChildRef', 'ParentRef']
writer.writerow(row)
folderpath = 'C:\Users\GCheves\aladin\Person-Files'
for filename in os.listdir(folderpath):
if filename.endswith('.xml'):
filepath = os.path.join(path, filename)
tree = ET.parse(filepath)
root = tree.getroot()
ns = {'aa': '
http://www.aladintech.com/pg/schema/export/'}
for child in root.findall(".//aa:object[@type='com.aladin.object.person']", ns):
ObjectType = child.attrib.get('type')
ObjectID = child.attrib.get('id')
#print(ObjectType)
#print(ObjectID)
for child in root.findall(".//aa:property[@type='com.aladin.property.fic.Ethnicity']", ns):
child = child.find('./aa:propertyValue/aa:propertyData', ns)
Ethnicity = child.text
#print(Ethnicity)
for child in root.findall(".//aa:property[@type='com.aladin.property.IntrinsicTitle']", ns):
child = child.find("./aa:propertyValue/aa:propertyComponent[@type='TITLE']/aa:propertyData", ns)
Title = child.text
#print(Title)
for child in root.findall(".//aa:property[@type='com.aladin.property.Address']", ns):
child = child.find("./aa:propertyValue/aa:propertyComponent[@type='ADDRESS1']/aa:propertyData", ns)
Address = child.text
#print(Address)
for child in root.findall(".//aa:property[@type='com.aladin.property.Address']", ns):
child = child.find("./aa:propertyValue/aa:propertyComponent[@type='CITY']/aa:propertyData", ns)
City = child.text
#print(City)
for child in root.findall(".//aa:property[@type='com.aladin.property.Address']", ns):
child = child.find("./aa:propertyValue/aa:propertyComponent[@type='STATE']/aa:propertyData", ns)
State = child.text
#print(State)
for child in root.findall(".//aa:property[@type='com.aladin.property.fic.Gender']", ns):
child = child.find('./aa:propertyValue/aa:propertyData', ns)
Gender = child.text
#print(Gender)
for child in root.findall(".//aa:property[@type='com.aladin.property.DateofBirth']", ns):
child = child.find('./aa:propertyValue/aa:propertyData', ns)
DateOfBirth = child.text
#print(DateOfBirth)
for child in root.findall(".//aa:property[@type='
com.aladin.property.Name']", ns):
child = child.find("./aa:propertyValue/aa:propertyComponent[@type='FIRST_NAME']/aa:propertyData", ns)
FirstName = child.text
#print(FirstName)
for child in root.findall(".//aa:property[@type='
com.aladin.property.Name']", ns):
child = child.find("./aa:propertyValue/aa:propertyComponent[@type='MIDDLE_NAME']/aa:propertyData", ns)
MiddleName = child.text
#print(MiddleName)
for child in root.findall(".//aa:property[@type='
com.aladin.property.Name']", ns):
child = child.find("./aa:propertyValue/aa:propertyComponent[@type='LAST_NAME']/aa:propertyData", ns)
LastName = child.text
#print(LastName)
for child in root.findall(".//aa:property[@type='com.aladin.property.Height']", ns):
child = child.find('./aa:propertyValue/aa:propertyData', ns)
Height = child.text
#print(Height)
for child in root.findall(".//aa:property[@type='com.aladin.property.Role']", ns):
child = child.find('./aa:propertyValue/aa:propertyData', ns)
PropertyRole = child.text
#print(PropertyRole)
for child in root.findall(".//aa:link[@type='com.aladin.link.ApparentPersonInCharge']", ns):
LinkType = child.attrib['type'].split('.')[-1]
ParentRef = child.attrib.get('parentRef')
ChildRef = child.attrib.get('childRef')
print(LinkType)
#print(ParentRef)
#print(ChildRef)
row = [ObjectType, ObjectID, Ethnicity, Title, Address, City, State, Gender, DateOfBirth, FirstName, MiddleName, Lastname, Height, PropertyRole, LinkType, ChildRef, ParentRef]
writer.writerow(row)
winsound.Beep(500, 1000)