87 lines
3.6 KiB
Python
87 lines
3.6 KiB
Python
import xml.etree.ElementTree as ET
|
|
import csv
|
|
import sys
|
|
import re
|
|
|
|
def clean_html(raw_html):
|
|
if not raw_html:
|
|
return ""
|
|
cleanr = re.compile('<.*?>')
|
|
cleantext = re.sub(cleanr, '', raw_html)
|
|
return cleantext.strip().replace('\n', ' ')
|
|
|
|
try:
|
|
tree = ET.parse('/home/x79/sisvietnamvn_01/sisvietnamvn_Trang chính thức hiện tại/bnhvinakhoaquctsiscnth.WordPress.2026-07-28 (5).xml')
|
|
root = tree.getroot()
|
|
except Exception as e:
|
|
print(f"Error parsing XML: {e}")
|
|
sys.exit(1)
|
|
|
|
ns = {
|
|
'wp': 'http://wordpress.org/export/1.2/',
|
|
'content': 'http://purl.org/rss/1.0/modules/content/'
|
|
}
|
|
|
|
# First pass to build a dictionary of attachment URLs
|
|
attachments = {}
|
|
for channel in root.findall('channel'):
|
|
for item in channel.findall('item'):
|
|
post_type_el = item.find('wp:post_type', ns)
|
|
if post_type_el is not None and post_type_el.text == 'attachment':
|
|
post_id_el = item.find('wp:post_id', ns)
|
|
attachment_url_el = item.find('wp:attachment_url', ns)
|
|
if post_id_el is not None and attachment_url_el is not None:
|
|
attachments[post_id_el.text] = attachment_url_el.text
|
|
|
|
csv_path = '/home/x79/sisvietnamvn_01/sisvietnamvn_main/doctors_from_xml.csv'
|
|
with open(csv_path, 'w', newline='', encoding='utf-8') as csvfile:
|
|
fieldnames = ['Name', 'Position', 'Gender', 'Birthday', 'Phone', 'Email', 'Address', 'Order', 'ImageID', 'ImageURL', 'Content']
|
|
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
|
writer.writeheader()
|
|
|
|
for channel in root.findall('channel'):
|
|
for item in channel.findall('item'):
|
|
post_type_el = item.find('wp:post_type', ns)
|
|
if post_type_el is not None and post_type_el.text == 'employee':
|
|
title = item.find('title')
|
|
name = title.text if title is not None else ""
|
|
|
|
content_el = item.find('content:encoded', ns)
|
|
content = clean_html(content_el.text) if content_el is not None else ""
|
|
|
|
doctor_data = {
|
|
'Name': name,
|
|
'Position': '',
|
|
'Gender': '',
|
|
'Birthday': '',
|
|
'Phone': '',
|
|
'Email': '',
|
|
'Address': '',
|
|
'Order': '',
|
|
'ImageID': '',
|
|
'ImageURL': '',
|
|
'Content': content[:200] # truncate for CSV view
|
|
}
|
|
|
|
for meta in item.findall('wp:postmeta', ns):
|
|
key_el = meta.find('wp:meta_key', ns)
|
|
val_el = meta.find('wp:meta_value', ns)
|
|
|
|
if key_el is not None and key_el.text and val_el is not None:
|
|
k = key_el.text
|
|
v = val_el.text or ""
|
|
if k == 'position': doctor_data['Position'] = v
|
|
elif k == 'gender': doctor_data['Gender'] = v
|
|
elif k == 'birthday': doctor_data['Birthday'] = v
|
|
elif k == 'phone_number': doctor_data['Phone'] = v
|
|
elif k == 'email': doctor_data['Email'] = v
|
|
elif k == 'address': doctor_data['Address'] = v
|
|
elif k == 'order': doctor_data['Order'] = v
|
|
elif k == 'image':
|
|
doctor_data['ImageID'] = v
|
|
doctor_data['ImageURL'] = attachments.get(v, '')
|
|
|
|
writer.writerow(doctor_data)
|
|
|
|
print(f"Successfully wrote CSV to {csv_path}")
|