- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmlparse.py
More file actions
Latest commit
91 lines (62 loc) · 2.17 KB
/
Copy pathxmlparse.py
File metadata and controls
91 lines (62 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
importos
importglob
importxml.etree.ElementTreeasET
importre
# Schema for xml temparing
schema="{urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2}"
schema_id="{urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2}"
# Variables to store the extracted order information
contact_final= []
address_final= []
order_id= []
# Getting latest file name to Parse
defget_latest_file_name():
files=glob.glob('./xml/*.xml')
returnmax(files, key=os.path.getctime)
# Load the file name in a variable
xml_file=get_latest_file_name()
# Reading the File
defload_xml(arg):
withopen(arg, 'r') asfile:
returnfile.read()
# Function for to extract ID, Contact & Delivery
defparse_xml():
root=ET.fromstring(load_xml(xml_file))
# Get Order ID
get_id=root.find("{}ID".format(schema_id))
order_id.append(get_id.text)
# Get Delivery
forget_deliveryinroot.find("{}Delivery".format(schema)):
foreleminlist(get_delivery.itertext()):
ifre.match("[ a-zA-Z0-9 ]", elem):
address_final.append(elem)
# Get Customer information
forcustomerinroot.find("{}AccountingCustomerParty".format(schema)):
forcontactinlist(customer.iterfind("{}Contact".format(schema))):
forthe_contactinlist(contact.itertext()):
ifre.match("[ a-zA-Z0-9 ]", the_contact):
contact_final.append(the_contact)
write_xml()
# Function to write the parced xml file
defwrite_xml():
# XML Fields Names
address_fields= ["Address", "City", "Postcode", "Country"]
contact_fields= ["Name", "Phone", "Email"]
# Root of XML
data=ET.Element("data")
data=ET.SubElement(data, "data")
# Write Order ID
id_details=ET.SubElement(data, "ID")
id_details.text=str(order_id[0])
# Write Order Contact
foriinrange(len(contact_final)):
person_details=ET.SubElement(data, contact_fields[i])
person_details.text=str(contact_final[i])
# Write Order Address
foriinrange(len(address_final)):
address=ET.SubElement(data, address_fields[i])
address.text=str(address_final[i])
# Write to file
tree=ET.ElementTree(data)
tree.write("order_{}.xml".format(order_id[0]),encoding="utf-8", xml_declaration=True)
parse_xml()