Python如果想從xml裏讀點信息,用BeautifulSoup可能會容易一點,可是若是要修改xml,BeatifulSoup就搞不定了,其實直接用lxml就好。app
1 from lxml import etree 2 3 tree = etree.parse("xxx.xml") 4 cfgs = tree.find('//component[@name="cmake-settings"]/configurations') 5 cfgs.clear() 6 cfgs.append(etree.Element("configuration", attrib={"CONFIG_NAME":"Debug"})) 7 tree.write("xxx.xml")
etree表示整個xml樹結構,對其元素修改,就直接表現爲對etree的修改,而後存儲便可。通常的函數用法現查便可,只有XPath須要瞭解下。目前我只瞭解了以一下幾項,基本夠用:函數