【问题标题】:variable in XML subelementXML 子元素中的变量
【发布时间】:2013-08-11 13:36:15
【问题描述】:

我正在考虑用 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>'

【问题讨论】:

    标签: python xml tree elementtree


    【解决方案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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-26
      相关资源
      最近更新 更多