1

I'm thinking of Python code to create a dynamic xml ETREE subElement.

I have a hierarchical header to describe a peace of book as the following:

<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>

How to get a hierarchical xml tag like this :

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

This is my code:

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')

I got this bad result:

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

1 回答 1

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')
于 2013-08-11T13:56:15.737 回答