Есть код.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, traceback, os
sys.path.insert(0, '../')
import pymysql.cursors
import xml.etree.ElementTree as ET
from xml.dom import minidom
def select():
connect = pymysql.connect(host='localhost', user='qwewqer', password='w1g-wqerwqr', db='wqerqweqwer', charset='utf8mb4')
product = "product"
try:
with connect.cursor() as cursor:
cursor.execute(""" select posts.ID, prodmeta.sku, posts.post_title, prodmeta.min_price, prodmeta.max_price, prodmeta.stock_quantity, prodmeta.stock_status
from cms_wp_posts as posts
right join cms_wp_wc_product_meta_lookup as prodmeta
on prodmeta.product_id = posts.ID
where posts.post_type = '%s' """ % product)
res = cursor.fetchall()
print(res)
if res:
count = 0
n = len(res)
print(n)
while count < n:
offer = ET.Element('offer')
isower_id = ET.SubElement(offer, 'isower_id')
isower_id.text = str(int(res[0]))
article = ET.SubElement(offer, 'article')
article.text = str(res[1])
title = ET.SubElement(offer, 'title')
title.text = str(res[2])
code = ET.SubElement(offer, 'code')
code.text = 'RU-ALT'
min_price = ET.SubElement(offer, 'min_price')
min_price.text = str(int(res[3]))
max_price = ET.SubElement(offer, 'max_price')
max_price.text = str(int(res[4]))
stock_id = ET.SubElement(offer, 'stock_id')
stock_id.text = str(res[5])
available = ET.SubElement(offer, 'available')
available.text = str(res[6])
city = ET.SubElement(offer, 'city')
city.text = 'Алтайскмй край, г. Бийск'
addres = ET.SubElement(offer, 'addres')
addres.text = 'отсутствует'
pickup = ET.SubElement(offer, 'pickup')
pickup.text = '1'
active = ET.SubElement(offer, 'active')
active.text = '1'
count += 1
save_xml('new.xml', offer)
else:
print("Container is empty!")
except Exception as e:
connect.close()
print("Ошибка: "+str(e)+"")
finally:
connect.close()
def save_xml(filename, xml_code):
xml_string = ET.tostring(xml_code).decode()
xml_prettyxml = minidom.parseString(xml_string).toprettyxml()
with open(filename, 'w') as xml_file:
xml_file.write(xml_prettyxml)
if __name__ == '__main__':
select()
Как сделать чтобы в XML загружалась информация из базы ?
Необходимо получить файл в таком формате
<?xml version="1.0" ?>
<offer>
<offers>
<isower_id>123</isower_id>
<article>тест1</article>
<title>фыаыва</title>
<code>RU-ALT</code>
<min_price>115.0</min_price>
<max_price>115.0</max_price>
<stock_id>12</stock_id>
<available>instock</available>
<city>Алтайскмй край, г. Бийск</city>
<addres>отсутствует</addres>
<pickup>1</pickup>
<active>1</active>
</offers>
<offers>
<isower_id>123</isower_id>
<article>тест2</article>
<title>йцукен</title>
<code>RU-ALT</code>
<min_price>116.0</min_price>
<max_price>117.0</max_price>
<stock_id>12</stock_id>
<available>instock</available>
<city>Алтайскмй край, г. Бийск</city>
<addres>отсутствует</addres>
<pickup>1</pickup>
<active>1</active>
</offers>
</offer>