3

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

1 回答 1

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>

child2打印精美的 XML 文档中的元素具有一个.tail\n后跟两个空格组成的属性。您可以通过repr(pp). 这两个空格是导致</root>结束标签错位的原因。

如果使用 parser 选项解析漂亮打印的 XML 文档remove_blank_text=True,则不会出现中断的纯空白元素尾部,并且最后一个(“第 2 部分”)漂亮打印将按预期工作。

也可以看看:

于 2013-05-15T17:44:16.173 回答