-1

PythonとLXMLを使用して、Mysqlクエリ結果からXMLファイルを作成しようとしています。これが私が欲しいフォーマットです。

     <DATA>
         <ROW>
             <FIELD1>content</FIELD1>
             <FIELD2>content</FIELD2>
         </ROW>
    </DATA>

何らかの理由で、コードが正しくフォーマットされておらず、XMLが検証されません。これがそのコードです

from lxml import etree
from lxml.etree import tostring
from lxml.builder import E
import MySQLdb


try:
       conn = MySQLdb.connect(host = 'host',user = 'user',passwd = 'pass',db = 'db')
       cursor = conn.cursor()
except:
       sys.exit(1)

cursor.execute("SELECT * FROM db.table")
columns = [i[0] for i in cursor.description]
allRows = cursor.fetchall()
xmlFile = open("mysqlxml.xml","w")
xmlFile.write('<DATA>')
for rows in allRows:
       xmlFile.write('<ROW>')
       columnNumber = 0
       for column in columns:
              data = rows[columnNumber]
              if data == None:
                     data = ''
              xmlFile.write('<%s>%s</%s>' % (column,data,column))
              columnNumber += 1
       xmlFile.write('</ROW>')
xmlFile.write('</DATA>')
xmlFile.close()
4

2 に答える 2

2

これは、lxmlを使用してxmlを構築する方法の小さな例です。

要素を作成するためのヘルパー関数を作成すると便利です。これは簡単なものです。デモ用にダミーのカーソルオブジェクトを作成しました。

from lxml import etree
from lxml.builder import E as buildE

class DummyCursor(object):
  def __init__(self,fields,rows=5):
    self.description = [[f] for f in fields]
    self.data = [ ["%s%02d" % (f,i) for f in fields] for i in range(rows) ]
  def fetchall(self):
    return self.data

def E(tag,parent=None,content=None):
  """Simple E helper"""
  element = buildE(tag)
  if content is not None:
    element.text = unicode(content)
  if parent is not None:
    parent.append(element)
  return element

def fetchXML(cursor):
  fields = [x[0] for x in cursor.description ]
  doc = E('data')
  for record in cursor.fetchall():
    r = E('row',parent=doc)
    for (k,v) in zip(fields,record):
      E(k,content=v,parent=r)
  return doc

doc = fetchXML(DummyCursor(['name','description']))

print etree.tostring(doc,pretty_print=True)

収量:

<data>
  <row>
    <name>name00</name>
    <description>description00</description>
  </row>
  <row>
    <name>name01</name>
    <description>description01</description>
  </row>
  <row>
    <name>name02</name>
    <description>description02</description>
  </row>
  <row>
    <name>name03</name>
    <description>description03</description>
  </row>
  <row>
    <name>name04</name>
    <description>description04</description>
  </row>
</data>
于 2012-04-04T15:29:09.200 に答える
0

AWS s3バケットで、Pythonとlxmlを使用してMySQLクエリからXMLを作成または更新します。

#----コーディング:utf-8 ---

from xml.etree import ElementTree
import json, pymysql, requests
import boto3
from boto3.s3.transfer import S3Transfer


BD = 'bd'
HOST = 'host'
USER = 'user'
PASS = 'pass'

HEADERS = {"Content-Type" : "application/json"}
AWS_ACCESS_KEY_ID = 'value_access_key'
AWS_SECRET_ACCESS_KEY = 'value_sercret_access_key'

def updateXMLFunction():

    db = pymysql.connect(HOST,USER,PASS,BD)     
    cursor = db.cursor()

    SQL = "SELECT * FROM table_name"

    cursor.execute(SQL)

    resp = {  
        "response": {
            "message" : "",
            "estatus" : "",
        }  
    }

    s3 = boto3.resource('s3',
         aws_access_key_id=AWS_ACCESS_KEY_ID,
         aws_secret_access_key= AWS_SECRET_ACCESS_KEY)
    bucket = s3.Bucket('name_carpet_bucket')
    object = bucket.Object('filename_heresavetheresult.xml')

    columns = [i[0] for i in cursor.description]
    allRows = cursor.fetchall()

    Document = ElementTree.Element("Document")
    try:
        for rows in allRows:
            Row = ElementTree.SubElement(Document, "Row")
            columnNumber = 0
            for column in columns:

                data = rows[columnNumber]
                if data == None:
                    data = ''
                data = str(data).replace('&', '\&')
                columnas = ('<%s>%s</%s>' % (column,data,column))   
                ElementTree.SubElement(Row, column).text = data
                columnNumber += 1

        xmlstr = ElementTree.tostring(Document, encoding='utf8', method='xml')
        # print(xmlstr)
        object.put(Body=xmlstr, ACL='public-read')

        resp["response"]["message"] = "Se ha creado el XML correctamente"
        resp["response"]["estatus"] = "updated"

        print(json.dumps(resp, indent=4, sort_keys=True))
        return resp

    except Exception as e:
        resp["response"]["message"] = e
        resp["response"]["estatus"] = "error"
        # print(json.dumps(resp, indent=4, sort_keys=True))
        return resp

updateXMLFunction()
于 2020-02-13T22:42:46.260 に答える