Python和ElementTree:如何在XML中嵌套元素/子元素?

时间:2015-03-04 17:55:36

标签: python xml dictionary python-2.6 elementtree

背景

我正在使用 Python 2.6 ElementTree SQLite3 。我的脚本目前执行以下操作:

  • 连接到数据库以从表/架构中检索信息
  • 向XML树添加必要的数据
  • 输出(当前不正确的)XML文件

代码

以下是我一直在检索架构数据并向XML添加元素的方法。我使用SOFTWARE_TARGET_表执行此操作。这是针对SOFTWARE_表:

software_attributes =  ["id", "functionalDesignationHardware", "hwfin", "identname", "partnumber",
                        "repfin", "targetHardwareID"]

software = db.cursor().execute("SELECT %s from SOFTWARE_" % ", ".join([i + "_" for i in software_attributes]))
software_Data = software.fetchall()
ID1 = db.cursor().execute("SELECT id_ from SOFTWARE_")
software_IDs = ID1.fetchall()

for sw in software_Data:
   sw_node = ET.SubElement(root, "Software")
   for s in range(1, len(software_attributes)):
      sw_node.set(software_attributes[s], str(sw[s]))

更新:这是我TARGET_表的代码:

target_attributes = ["id", "functionalDesignationSoftware", "installscriptpathname", "ata", "status",
                     "swfin", "targetOSFC", "timestamp"]

target = db.cursor().execute("SELECT %s from TARGET_" % ", ".join([i + "_" for i in target_attributes]))
target_Data = target.fetchall()
ID2 = db.cursor().execute("SELECT id_ from TARGET_")
target_IDs = ID2.fetchall()

## CURRENTLY INCORRECT - only adds to the last created Software Element ##
for tg in target_Data:
   tg_node = ET.SubElement(sw_node, "TargetModule")
   for t in range(1, len(target_attributes)):
      tg_node.set(target_attributes[t], str(tg[t]))

这就是我存储来自表的信息的方式,这些表的唯一目的是连接来自其他表的数据。 SOFTWARE_TARGET_表格将SOFTWARE_TARGET_相关联。我将其信息存储在字典中:

software_target = db.cursor().execute("SELECT SOFTWARE1, TARGET2 from SOFTWARE_TARGET_")
software_target_Data = software_target.fetchall()

# Map SOFTWARE1 to TARGET2
separated_st_Data = {}
for item in software_target_Data:
    software1, target2 = item
    try:
        separated_st_Data[software1].append(target2)
    except KeyError:
        separated_st_Data[software1] = [target2]

的尝试

到目前为止,我已经找到了如何以下列格式设置我的xml:

<Software attribute="stuff" attribute2="Stuff"/>
<Software attribute="stuff" attribute2="Stuff"/>
<Target attribute="things" attribute2="Things"/>

但我需要的是以下格式:

<Software attribute="stuff" attribute2="Stuff"
    <Target attribute="things" attribute2="Things"/>
    <Target attribute="things" attribute2="Things"/>
</Software>
<Software attribute="stuff" attribute2="Stuff"/>

Target个子元素位于哪个Software元素由SOFTWARE_TARGET_表中的信息确定。我已经找到了如何遍历我的字典,如下:

depth=0
for k,v in sorted(separated_st_Data.items(),key=lambda x: x[0]):
    if isinstance(v, dict):
        print ("  ")*depth + ("%s" % k)
        walk_dict(v,depth+1)
    else:
        print ("  ")*depth + "%s %s" % (k, v)

问题

如何根据数据库表中的信息创建格式正确的XML文件(如 Attemps 部分所述)?我创建了字典,以为我可以将它用于此目的 - 如果有必要,请告诉我。


备注

This是我从SOFTWARE_TARGET_表创建的字典的样子。键代表id_中的SOFTWARE_架构,值代表id_中的TARGET_架构。 (请告诉我,如果我的术语听起来有点 - 数据库有时让我困惑)。

2 个答案:

答案 0 :(得分:1)

创建Target元素时(此处未在问题中提供代码),请务必将他们想要附加的sw_node作为第一个参数传递。

那是:

target_el = SubElement(sw_node, "Target")

而不是......

target_el = SubElement(root_node, "Target")

此类代码的典型模式可能具有以下外观(粗略;需要一些测试,使用pyformat参数样式为DB-API驱动程序编写,并且不与其他人一起工作):< / p>

cursor = db.cursor()
cursor.execute("SELECT * from SOFTWARE_")
for sw_item in cursor.fetchall():
  sw_el = SubElement(root_el, 'Software') ## STORING THE ELEMENT HERE
  sw_id = None
  for idx in range(len(cursor.description)):
    name = cursor.description[idx][0]
    if name == 'id':
      sw_id = sw_item[idx]
    sw_el.attrib[name] = sw_item[idx]
  ## QUERYING FOR CHILDREN HERE
  cursor.execute("SELECT TARGET_.*
                  FROM TARGET_, SOFTWARE_TARGET_
                  WHERE SOFTWARE_TARGET_.SOFTWARE1=%(sw_id)s
                    AND SOFTWARE_TARGET_.TARGET2=TARGET_.ID",
      sw_id=sw_id)
  for target_item in cursor.fetchall():
    # create a new target element
    target_el = SubElement(sw_el, 'Target')
    # assign attributes to that element
    for idx in range(len(cursor.description)):
      name = cursor.description[idx][0].rsplit('.', 1)[-1]
      target_el.attrib[name] = target_item[idx]

答案 1 :(得分:0)

哎呀,差点忘了 - 这是最终代码我最终在完成的Python脚本中使用(当然,减去完整脚本的几个关键元素):

software_attributes =  ["id", "partnumber", "identname", "functionalDesignationHardware", "hwfin", 
                        "targetHardwareID","repfin", "amendment"]

target_attributes = ["id", "swfin", "targetOSFC", "functionalDesignationSoftware", "installscriptpathname", 
                     "ata", "status","timestamp"]

sw_current = cursor.execute("SELECT %s from SOFTWARE_" % ", ".join([i + "_" for i in software_attributes]))
sw_current = sw_current.fetchall()
for sw_item in sw_current:
    current_sw_ID = sw_item[0]

    # Create Software XML Element
    sw_element = ET.SubElement(root, "Software")
    # Set Software attributes
    for s in range(1, len(software_attributes)):
        sw_element.set(software_attributes[s], str(sw_item[s]))

    # Get all Target IDs for current Software Element
    current_tg_IDs = cursor.execute("SELECT TARGET2 from SOFTWARE_TARGET_ WHERE SOFTWARE1=?", (current_sw_ID,))
    current_tg_IDs = list(chain.from_iterable(current_tg_IDs.fetchall()))
    while len(current_tg_IDs) > 0:
        tg_id = current_tg_IDs.pop(0)
        tg_current = cursor.execute("SELECT %s from TARGET_ WHERE id_=?" % ", ".join([i + "_" for i in target_attributes]), (str(tg_id).strip('[]'),))
        tg_current = tg_current.fetchall()

        for tg_item in tg_current:
            # Create Target XML Element
            tg_element = ET.SubElement(sw_element, "TargetModule")
            # Set Target attributes
            for t in range(1, len(target_attributes)):
                tg_element.set(target_attributes[t], str(tg_item[t]))

注意:在上面的最后一次尝试中,我最终没有使用最初在此问题中假设的字典方法 - 我的最终方法对我的目的更有效。有关使用词典的方法示例,请参阅此问题的选定答案。