我是 LXML 的新手,在解析我的元素后遇到了一个问题:如果我删除(或替换)最后一个孩子,它的架构似乎已经改变。这是我的代码。
(对不起,我是stackoverflow的新手,所以我不能发布图片)
我一直在寻找解决方案,但我仍然无法弄清楚我做错了什么。我真的很感激有人的帮助!(我在 Windows 上使用 LXML 3.2.1 和 Python 2.6)
from lxml import etree
from copy import deepcopy
def Write( file, element ):
f = open( file, 'w' )
f.write( etree.tostring( element, xml_declaration=True, encoding="ISO-8859-1", pretty_print = True ) )
f.close()
return 1
def ReadAndReturn( file ):
lookup = etree.ElementDefaultClassLookup()
parser = etree.XMLParser(recover = True)
parser.set_element_class_lookup( lookup )
mainTree = etree.parse( file, parser )
return mainTree
# create a root element with 3 children
root = etree.Element( "root" )
root.append( etree.Element( "child1" ) )
child2 = etree.SubElement( root, "child2" )
child2.text = 'CHILD2'
child3 = etree.SubElement( root, "child3" )
child3.text = 'CHILD3'
print "\n--- INITIAL ROOT ---"
print( etree.tostring( root, pretty_print=True ) )
# remove last child
root2 = deepcopy( root )
root2.remove( root2[2] )
print "--- ROOT WITHOUT LAST CHILD / BEFORE WRITING ---"
print( etree.tostring( root2, pretty_print=True ) )
# write initial root (3 children) and read the file
filename = 'test.tst'
status = Write( filename, root )
tree = ReadAndReturn( filename )
# remove last child from the read element
root3 = deepcopy( tree.getroot() )
root3.remove( root3[2] )
print "--- ROOT WITHOUT LAST CHILD / AFTER WRITING AND PARSING ---"
print( etree.tostring( root3, pretty_print=True ) )