一、前言
本节是修改xml;同样方法也可修改arxml文件;主要使用LET.Element创建节点;
二、知识点
1、专门用于格式化处理xml文件使其具有层级分明的缩进函数
#专门用于格式化处理xml文件使其具有层级分明的缩进
def indent( elem, level=0):
i = "\n" + level * " "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level + 1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
2、修改XML文件
#举例1:添加节点'country'
import lxml.etree as LET
tree = LET.parse('country_data.xml')
root = tree.getroot()
"""
country_ref = LET.Element("neighbor")
country_ref.attrib['name'] = 'china'
country =root.findall('country')
print(country)
country[0].append(country_ref)
indent(root)
tree.write("d:/myself/write_arxml/country_data.xml", xml_declaration=True, encoding='utf-8')
"""
#alt+shift+A 整段注销
3、修改节点rank文本值,添加属性
#举例2:修改节点rank文本值,添加属性
"""
for rank in root.iter('rank'):
new_rank = int(rank.text) + 1
rank.text = str(new_rank)
rank.set('updated', 'yes')
tree.write('output.xml')
"""
4、删除节点
#举例3 删除节点
"""
for country in root.findall('country'):
# using root.findall() to avoid removal during traversal
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
tree.write('output.xml')
"""
5、 构建 XML 文档
#知识点4:构建 XML 文档¶
""" a = LET.Element('a')
b = LET.SubElement(a, 'b')
c = LET.SubElement(a, 'c')
d = LET.SubElement(c, 'd')
LET.dump(a) #Writes an element tree or element structure to sys.stdout.
"""
三、实例 代码
-
- import lxml.etree as LET
- #专门用于格式化处理xml文件使其具有层级分明的缩进
- def indent( elem, level=0):
- i = "\n" + level * " "
- if len(elem):
- if not elem.text or not elem.text.strip():
- elem.text = i + " "
- if not elem.tail or not elem.tail.strip():
- elem.tail = i
- for elem in elem:
- indent(elem, level + 1)
- if not elem.tail or not elem.tail.strip():
- elem.tail = i
- else:
- if level and (not elem.tail or not elem.tail.strip()):
- elem.tail = i
- #alt+shift+A 整段注销
- #可以通过从文件中读取来导入此数据:
- tree = LET.parse('country_data.xml')
- root = tree.getroot()
- #可以迭代的子节点
- """ for child in root:
- print(child.tag, child.attrib) """
- #子级是可以嵌套的,我们可以通过索引访问特定的子级节点
- """ print(root[0][2].text) """
- #递归遍历其下的所有子树(包括子级,子级的子级,等等)
-
- """ for neighbor in root.iter('neighbor'):
- print(neighbor.attrib) """
- print("find:===============")
- """ for neighbor in root.findall('neighbor'):
- #rank = country.find('rank').text
- #name = country.get('name')
- print(neighbor.attrib) """
-
- #print(name, rank)
- """ for neighbor in root.xpath('country'):
- print(neighbor.attrib)
- test = neighbor.getparent().find('rank').text
- print(test) """
- country_ref = LET.Element("neighbor")
- country_ref.attrib['name'] = 'china'
-
- country =root.findall('country')
- print(country)
- country[0].append(country_ref)
- indent(root)
- tree.write("d:/myself/write_arxml/country_data.xml", xml_declaration=True, encoding='utf-8')
-
四、运行代码后


五、测试用 xml文件
- <data>
- <country name="Liechtenstein">
- <rank>1rank>
- <year>2008year>
- <gdppc>141100gdppc>
- <neighbor name="Austria" direction="E"/>
- <neighbor name="Switzerland" direction="W"/>
- country>
- <country name="Singapore">
- <rank>4rank>
- <year>2011year>
- <gdppc>59900gdppc>
- <neighbor name="Malaysia" direction="N"/>
- country>
- <country name="Panama">
- <rank>68rank>
- <year>2011year>
- <gdppc>13600gdppc>
- <neighbor name="Costa Rica" direction="W"/>
- <neighbor name="Colombia" direction="E"/>
- country>
- data>