XML子元素中的变量

时间:2013-08-11 13:36:15

标签: python xml tree elementtree

我正在考虑使用Python代码来创建动态xml ETREE子元素。

我有一个层级标题来描述书籍的和平如下:

<Books>
<Booktype List= "Story > Fiction > Young">
#here the rest of book text
</Booktype>
<Booktype List= "Science > Math > Young">
#here the rest of book text
</Booktype>
</Books>

如何获得这样的分层xml标记:

<Books>
<Booktype>
  <Story>
    <Fiction>
         <Young>
#here the rest of book text
         </Young>
    </Fiction>
  </Story>
</Booktype>
</Books>

这是我的代码:

import re
import xml.etree.ElementTree as ET
from xml.etree import ElementTree


List= "Story>Fiction>Young"
List = List.split('>')
root = ET.Element('Books')
Booktype =ET.SubElement(root,'Booktype')

for l in List:
  ND = ET.SubElement(Booktype,str(l))
  Booktype.append(ND)

tree = ET.ElementTree(root)
ElementTree.tostring(root,'utf-8')

我得到了这个糟糕的结果:

'<Books><Booktype><Story /><Story /><Story /><Fiction /><Fiction /><Young /><Young /><Story /><Story /><Fiction /><Fiction /><Young /><Young /></Booktype></Books>'

1 个答案:

答案 0 :(得分:1)

如果要嵌套列表元素,则必须保留对前一个元素的引用,以便可以将子元素添加到它,而不是Booktype元素。请参阅示例中的变量currrent

from xml.etree import ElementTree as ET

xml_string = '''<Books>
<Booktype List= "Story > Fiction > Young">
#here the rest of book text
</Booktype>
<Booktype List= "Science > Math > Young">
#here the rest of book text 2
</Booktype>
</Books>
'''

xml = ET.fromstring(xml_string)
for booktype in xml.findall('Booktype'):
    types = map(lambda x: x.strip(), booktype.get('List').split('>'))
    current = booktype
    for t in types:
        current = ET.SubElement(current, t)
    current.text = booktype.text
    booktype.text = ''
    del booktype.attrib['List']
print ET.tostring(xml,'utf-8')

给我结果:

<Books>
<Booktype><Story><Fiction><Young>
#here the rest of book text
</Young></Fiction></Story></Booktype>
<Booktype><Science><Math><Young>
#here the rest of book text 2
</Young></Math></Science></Booktype>
</Books>

如果你想创建一个全新的结构,你可以这样做:

xml = ET.fromstring(xml_string)
root = ET.Element('Books')
for booktype in xml.findall('Booktype'):
    current = ET.SubElement(root, 'Booktype')
    for t in map(lambda x: x.strip(), booktype.get('List').split('>')):
        current = ET.SubElement(current, t)
    current.text = booktype.text
print ET.tostring(root, 'utf-8')