从漂亮的XML中删除元素后留下了不需要的空白

时间:2013-05-15 13:12:32

标签: python lxml

我是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 ) )

1 个答案:

答案 0 :(得分:0)

空白处理可能很棘手。以下是您的程序的简化版本,用于演示正在进行的操作。

from lxml import etree

# 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 the "ugly" XML (no whitespace)
print "\n--- UGLY ---"
print etree.tostring(root)

# Print the "pretty" XML
print "\n--- PRETTY ---"
pp = etree.tostring(root, pretty_print=True)
print pp

# Parse the pretty XML
tree = etree.fromstring(pp) 

# remove last child
tree.remove(tree[2])

print "--- WITHOUT LAST CHILD PART 1 ---"
print etree.tostring(tree, pretty_print=True)

# Parse the pretty XML once again with parser option 'remove_blank_text=True'
tree = etree.fromstring(pp, etree.XMLParser(remove_blank_text=True)) 

# remove last child
tree.remove(tree[2])

print "--- WITHOUT LAST CHILD PART 2 ---"
print etree.tostring(tree, pretty_print=True)

输出:

--- UGLY ---
<root><child1/><child2>CHILD2</child2><child3>CHILD3</child3></root>

--- PRETTY ---
<root>
  <child1/>
  <child2>CHILD2</child2>
  <child3>CHILD3</child3>
</root>

--- WITHOUT LAST CHILD PART 1 ---
<root>
  <child1/>
  <child2>CHILD2</child2>
  </root>

--- WITHOUT LAST CHILD PART 2 ---
<root>
  <child1/>
  <child2>CHILD2</child2>
</root>

漂亮打印的XML文档中的child2元素具有.tail属性,该属性由\n后跟两个空格组成。您可以通过repr(pp)进行检查。这两个空格是导致</root>结束标记未对齐的原因。

如果使用解析器选项remove_blank_text=True解析漂亮打印的XML文档,那么将不会破坏仅空白元素尾部,并且最后一个(“PART 2”)漂亮打印将按预期工作。

另见: