python lxml处理xml

解析xml

xml元素说明
<?xml version="1.0" encoding="utf-8"?>
<root>
    <tag1 attrib11="" attrib12="" >text1</tag1>tail1
    <tag2 attrib21="" attrib22="" >text2</tag2>tail2
</root>
生成xml对象
# encoding=utf8
from lxml import etree
xml = etree.parse('filepath')
找到根节点,获取所有子节点
root = xml.getroot()
children = root.xpath('//*')
child = children[0]

etree.parse()得到对象lxml.etree._ElementTreexml.getroot()得到对象Element,我们主要操作的对象是Element

Element属性
print(child.tag)
print(child.attrib)
print(child.text)
print(child.tail)

>>> tag1
>>> {
    
    'attrib11': '', 'attrib12': ''}
>>> text1
>>> tail1

Element操作

修改属性
child.tag = 'newtag'
newattrib = {
    
    }
child.attrib.update(newattrib)

attrib属性不能直接赋值,可用dict.update方法来修改

新建Element
attrib = {
    
    }
element = etree.Element('tag', attrib=attrib)
Element子节点增删改
print(root.xpath('.//*'))
root.append(child)
print(root.xpath('.//*'))
root.insert(0, child)
print(root.xpath('.//*'))
root.remove(child)
print(root.xpath('.//*'))
root.append(element)
print(root.xpath('.//*'))

>>> [<Element t8aaa at 0x52e8ac8>, <Element t98ab at 0x52e8b08>]
>>> [<Element t98ab at 0x52e8b08>, <Element t8aaa at 0x52e8ac8>]
>>> [<Element t8aaa at 0x52e8ac8>, <Element t98ab at 0x52e8b08>]
>>> [<Element t98ab at 0x52e8b08>]
>>> [<Element t98ab at 0x52e8b08>, <Element t7efa at 0x52e8bf8>]

在同一个父节点下插入同一个Element只会改变顺序,不会新增节点,只有插入新的Element才会新增节点

生成xml文件

etree.ElementTree(root).write('filename', encoding='utf-8', pretty_print=True, 
							  xml_declaration=True)
  • encoding控制xml编码,不设置时默认使用URL编码。
  • xml_declaretion控制xml是否带声明(<?xml version="1.0" encoding="utf-8"?>)。
  • pretty_print控制是否带格式输出,需要特别注意的是,要使pretty_print生效,需要在解析xml的时候就设置使用参数为remove_blank_text=True的解析器1,即:
xml = etree.parse('filepath', parser=parser=etree.XMLParser(remove_blank_text=True))
root = xml.getroot()

etree.Element生成的节点作为根节点生成xml则pretty_print可以生效

参考文献


  1. https://lxml.de/FAQ.html#why-doesn-t-the-pretty-print-option-reformat-my-xml-output ↩︎

猜你喜欢

转载自blog.csdn.net/chinesesexyman/article/details/106639441